Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Color specifics words in a JTextPane

 
Antonio Ruffolo
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to color some words as soon as they are typed.
This is the code.



I use a Key_release listener.
When it catches the event i do this:
punt the text in a string
check that there is the word that I want to color
start to color it in a while(true) cycle, which ends when there aren't any other occurrences of the word.


My problem is that it doesn't color only the range that I specified, but al the rest of the text that i type will have that color.
For this reason wrote ne next two lines


The problem is, that in this way it colors only a word, I can't use it to color more different words.
If you try to run the code, it will color the word "prova", but if you remove the comments surrounding this if
It won't color the word ADD.

Do you have any idea of how to do this?
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DocumentListener is a better choice than KeyListener, as it also will be triggered when pasting text.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic