Java JTextPane Change Font of Selected Text

瘦欲@ 提交于 2019-12-01 06:22:05

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 .

You can do this by using JTextPane. It is impossible to do this using JTextArea. . Here is a best example on how to use JTextPane.

Sample: http://download.oracle.com/javase/tutorial/uiswing/components/editorpane.html

Code: http://download.oracle.com/javase/tutorial/uiswing/examples/components/TextSamplerDemoProject/src/components/TextSamplerDemo.java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!