Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlighter vs. StyledDocument of Java Swing for changing the color of text in a JTextPane

I've been using setCharacterAttributes of StyledDocument to change the colors of specific words or sections in a JTextPane.

But then I noticed some answers here where they use a Highlighter (typically DefaultHighlighter) to do the same thing.

What are the essential differences between the two approaches? What are some purposes for which using a highligher is better than manipulating the StyledDocument?

like image 220
Gigatron Avatar asked Dec 05 '25 05:12

Gigatron


1 Answers

The main difference is changing model (Document) in the first case and changing view in the second. If you need just colorizing text (background or foreground) it's better to use highlighter. If the font attributes change sizes use Document.

Also if you want to save some text with font properties use Document.

like image 142
StanislavL Avatar answered Dec 07 '25 20:12

StanislavL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!