2 Attachment(s)
JTextPane changing text color issues
Hi everyone, i'm new and I am struggling with a problem, please anyone help.
I created a GUI using JTextPane and i've got it working, I have created a working trie tree and I am trying to implement a realtime spellchecker.
What my program does according to my logic at the moment is if a word is not found in my trie, the word is changed to the color red.
I know my trie is working because I system out the words that aren't in the TRIE and it does so correctly. Now when I go into the if statement for when a word isn't in the trie I try the setCharacterAttributes method on the word from its starting index to the end of the word. the indexes work because I output them too and they are correct, I counted them myself to make sure, here is my code:
Code :
System.out.println("Misspelled words: ");
int indexS = 0, indexE = 0;
String all = createGui.ta.getText();
SimpleAttributeSet attrs = new SimpleAttributeSet();;
StyleConstants.setForeground(attrs, Color.red);
StyledDocument doc = createGui.ta.getStyledDocument();
while(true)
{
readWord(fIn);
if(!trie.found(s))
{
indexS = all.indexOf(s.toLowerCase());
System.out.println(s + " on line: " + lineNum);
indexE = indexS+s.length()-1;
System.out.println("indexS is at: " + indexS);
System.out.println("indexE is at: " + indexE);
doc.setCharacterAttributes(indexS,indexE, attrs, false);
}
if(ch == -1)
break;
}
Oh and by the way this is when I open an existing text file.
Here is the GUI and output I receive from netbeans:
Attachment 1215Attachment 1216
As you can see it isn't highlighting correctly, I'm not sure why, please help I've been struggling for hours.
Re: JTextPane changing text color issues