JTextPane 附加 HTML 字符串 [英] JTextPane append HTML string
本文介绍了JTextPane 附加 HTML 字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以在没有任何 HTML 问题的情况下解析 JTextPane 的内容:
I can parse the content of a JTextPane witout any problems in HTML:
textPane = new JTextPane();
textPane.setContentType("text/html");
textPane.setText(<b>Hello!</b>);
// ...
setVisible(true);
结果是
你好!
但是每当我尝试将字符串附加到 textPane 时,使用
But whenever I try to append a String to textPane, using
styledDoc = (StyledDocument) textPane.getStyledDocument();
styledDoc.insertString(styledDoc .getLength(), <b>Goodbye!</b>, null );
(如在这个问题中),我的输出是
你好! 再见!
(没有空格) - 所以 html 格式被跳过.
(without whitespaces) - so the html formatting is skipped.
如何将字符串附加到我的 JTextPane 对象并保留添加部分的 HTML 格式?
How can I append a String to my JTextPane Object and keep the HTML formation for the added part?
推荐答案
使用例如
HTMLDocument doc=(HTMLDocument) textPane.getStyledDocument();
doc.insertAfterEnd(doc.getCharacterElement(doc.getLength()),"<b>Goodbye!</b>");
或者
HTMLEditorKit kit=(HTMLEditorKit )textPane.getEditorKit();
如果您想插入段落/表格或其他分支元素,请使用该方法
and use the method if you would like to insert paragraph/table or another branch element
public void insertHTML(HTMLDocument doc, int offset, String html,
int popDepth, int pushDepth,
HTML.Tag insertTag)
这篇关于JTextPane 附加 HTML 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文