Philip Cando
Philip Cando

Reputation: 3

Inaccurate color changing of text in JTextPane

Tried using this code but it does not change colors accurately, notice the word stop. This happens as you type the words.

https://i.sstatic.net/Wrel4.png

https://stackoverflow.com/a/28773736/7694892

Upvotes: 0

Views: 64

Answers (1)

camickr
camickr

Reputation: 324108

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.

Upvotes: 1

Related Questions