【发布时间】:2011-10-31 16:00:13
【问题描述】:
我们正在尝试使用JTextPane 呈现HTML 和纯文本。基本上实际内容托管在远程服务器上,该内容可以包含一定程度的HTML 标签或根本不包含。在我的JTextPane 中,我将其定义如下:
JTextPane jText = new JTextPane();
jText.setEditable(false);
jText.setContentType("text/html");
String content = "Please view article <a href=mydomain.com/content.txt>Link..</a>";
jText.setText(content);
然后使用HyperlinkListener希望能够在点击链接时呈现内容。我正在使用下面的语法这样做;
jText.addHyperlinkListener(new HyperlinkListener()
{
public void hyperlinkUpdate(final HyperlinkEvent he)
{
//Render the page
try{
setPage(he.getURL()); //Error on this line
}catch(Exception e){e.printStackTrace();}
}
});
不幸的是,当我们单击链接以呈现内容时,我们最终会出现异常:
java.lang.IllegalArgumentException: Must be StyledEditorKit
at javax.swing.JTextPane.setEditorKit(JTextPane.java:474)
at javax.swing.JEditorPane.setContentType(JEditorPane.java:888)
at javax.swing.JEditorPane.getStream(JEditorPane.java:713)
at javax.swing.JEditorPane.setPage(JEditorPane.java:408)
当内容没有可用的 HTML 标记时,这看起来像。有人可以帮我们解决这个问题,以便我们可以呈现纯文本和 HTML。
提前致谢。
已编辑。
【问题讨论】:
-
无法重现,按预期正常工作