Tried using this code but it does not change colors accurately, notice the word stop. This happens as you type the words.
Asked
Active
Viewed 54 times
0
-
Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. Use the "edit" link to improve your *question* - do not add more information via comments. Thanks! – GhostCat Sep 29 '17 at 07:37
-
Do not link to code: add it to your question as nicely formatted text. – GhostCat Sep 29 '17 at 07:37
1 Answers
1
It would seem to me that you have a problem with the index. It is off by one as you move to the second row.
This would suggest to me that you are using textPane.getText()
to get the text to tokenize.
A solution would be to get the text from the Document directly:
int length = textPane.getDocument().getLength();
String text = textPane.getDocument().getText(0, length);
Check out Text and New Lines for the difference between the two approaches.
Basically the index will be off by one for each row since the string contains "\r\n" for each newline but the Document only contains "\n".
If this doesn't help then you need to do your own debugging of the code to find out why the offset is wrong.

camickr
- 321,443
- 19
- 166
- 288
-
Thanks! This worked for me, never really knew about those special characters for different text editors in java swings. – Philip Cando Sep 30 '17 at 15:54