JTextPane的追加HTML字符串 [英] JTextPane append HTML string

查看:1590
本文介绍了JTextPane的追加HTML字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以分析JTextPane中的不用其他HTML中的任何问题内容:

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 );

(在这个问题看到),我的输出

(as seen in this question), my output is

您好 &LT;!B&GT;!再见&LT; / B&GT;

(无空格) - 所以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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆