Java JTextPane Change Font of Selected Text

后端 未结 2 1931
梦毁少年i
梦毁少年i 2021-01-13 20:53

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

textpane.getSelectedText()

2条回答
  •  醉酒成梦
    2021-01-13 21:30

    You can change JTextPane's font only as a whole, it doesn't do rich text.

    There's a Document underneath JEditorPane (and apparently JTextPane too), which you get a hold of with getDocument(). You want to cast that to a StyledDocument if you can, and then you can do things like setCharacterAttributes to a given run of characters.

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

提交回复
热议问题