Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top