Java JTextPane Changes the font of selected text

I have a JTextPane (or JEditorPane, I can use either without problems). How to change the font of the selected area to a specific font?

textpane.getSelectedText().setFont() will not work. (Even with the font family)

+4
source share
2 answers

You can change the font of JTextPane only in general, it does not have rich text.

There is a Document under JEditorPane (and, obviously, JTextPane), which you get with getDocument() . You want to do this with StyledDocument if you can, and then you can do something like setCharacterAttributes for a given character range.

There are several (hopefully) useful examples in the Java tutorial: http://java.sun.com/docs/books/tutorial/uiswing/components/editorpane.html .

+4
source

All Articles