Styling text in a JTextArea or JTextPane

泪湿孤枕 提交于 2019-12-04 23:24:58
mKorbel

If you want to diplaying Html contents in the JTextPane then you have to set for JTextPane#setContentType("text/html");, example here

EDIT:

for JEditorPanes / JTextPanes is there another way by implements StyledDocument, MutableAttributeSet and with customized Highlighter, example here

a.m. way is without using Html syntax

Let Java know it will be HTML using setContentType method.

k.setContentType("text/html"); 

I use a Look and Feel (Substance) and calling setContentType("text/html") has problems with the font displayed. I solved it by calling:

textPane.putClientProperty(JTextPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);

Another option is wrapping the HTML text in a JLabel. Following your example the code is:

JTextPane k = new JTextPane();
k.insertComponent(new JLabel("<html><strong>Hey!</strong></html>"));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!