Highlighter vs StyledDocument Java Swing to change text color in JTextPane

I used setCharacterAttributes

to StyledDocument

to change the colors of certain words or sections in JTextPane


But then I noticed some answers here where they use Highlighter

(usually DefaultHighlighter

) to do the same.

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


source to share

1 answer

The main difference is changing the model (Document) in the first case and changing the view in the second. If you only want coloring text (background or foreground), it is better to use a marker. If font attributes change sizes, use Document.

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



All Articles