How to output using StyledDocument with HTML?

大憨熊 提交于 2019-12-10 10:38:40

问题


I have a JTextPane, and I would like to output text on it using StyledDocument. Here is my StyledDocument object

    StyledDocument dox = (StyledDocument) textArea.getDocument();

    Style style = dox.addStyle("StyleName", null);

    StyleConstants.setFontFamily(style, Font.SANS_SERIF);
    StyleConstants.setFontSize(style, 8);
    dox.insertString(dox.getLength(), "<b>Some Text</b>", null);

The problem right now is if I edit the text with html code, it does not display the way I want. I want the text to be displayed as bolded instead of literally "Some Text".

Is there a way to do this?


回答1:


I did figure it out on my own in the end using HTMLEditorKit, here's the answer for futher reference

    StyledDocument dox = (StyledDocument) textArea.getDocument();
    textPane.setEditorKit(new HTMLEditorKit());

    textPane.setText("<b>Some Text</b>");


来源:https://stackoverflow.com/questions/8290292/how-to-output-using-styleddocument-with-html

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