2 Attachment(s)
JTetPane changing text colour help
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 3719Attachment 3720
As you can see it isn't highlighting correctly, I'm not sure why, please help I've been struggling for hours.
Re: JTetPane changing text colour help
To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem. No spellcheck code, just a JTextPane populated with some text, and a button with an ActionListener that finds and highlights certain words in the text pane. Include your debug sysouts in the SSCCE.
db
Re: JTetPane changing text colour help
Re: JTetPane changing text colour help
Thanks Kevin. I'm outta here.
db