1

I am making a text editor and the last step is to create a simple spell checker. I have managed to work out how find incorrect words but now I would like to format those words so that they are shown to be incorrect example underlining them or highlighting them.

How can you format one word in a jtextarea() differently to the rest?

public void Spell_Check()
    {

    String[] english = new String[26871];
    String[] text_words = ((JTextArea) TabPane.getSelectedComponent()).getText().split(" "); 

     int count_words = 0;
     try {
        BufferedReader br = new BufferedReader(new FileReader("english.txt"));


        String lineFromFile = "";


       for(int i = 0; (lineFromFile = br.readLine()) != null; i++)
       {
         if(text_words[i] != lineFromFile )
         {

          text_words[i].setAttributes(51,  7, false);   
         }
       }

    } catch (Exception e) {
    }
}
kleopatra
  • 51,061
  • 28
  • 99
  • 211

1 Answers1

1

Take a look at question Delayed text color change in Swing text field where an answer is given on how to achieve this in a JTextField. And since the answer uses methods available on JTextComponent you can uses this for a JTextArea as well.

An even better example would be the Swing tutorial, and more specifically the TextFieldDemo which does almost exactly what you want

Community
  • 1
  • 1
Robin
  • 36,233
  • 5
  • 47
  • 99