I am making a GUI dictionary program and have an input of letters that need to be highlighted in each words that is display. I was wondering how to do this, it doesn't seem very hard to color my entire JTextArea, but coloring only certain characters seems to be a little more difficult. I have read about JTextPane where I can use styled fonts, but even that doesn't seem straightforward. I need to parse every dictionary word's character and then change that characters color.
Asked
Active
Viewed 322 times
3
-
See also this [example](http://stackoverflow.com/a/8534162/230513). – trashgod Jan 03 '12 at 12:09
2 Answers
5
Yes, you'd want to use JEditorPane
or JTextPane
to do this sort of thing. You can either use styles, as you've seen, or you could create the contents as HTML and display that, which would be a lot simpler, if less flexible.
The relevant section of the Java Tutorial starts here.

StanislavL
- 56,971
- 9
- 68
- 98

Ernest Friedman-Hill
- 80,601
- 10
- 150
- 186
2
You can add your own Highlights. See DefaultHighlighter and HighlightPainter.
See the http://java-sl.com/tip_vertical_selection.html example of custom highlights using. You can define any desired colors.

StanislavL
- 56,971
- 9
- 68
- 98