简体   繁体   English

java.lang.IllegalArgumentException:必须为StyledEditorKit

[英]java.lang.IllegalArgumentException: Must be StyledEditorKit

We are trying to render both HTML and plain text using JTextPane . 我们正在尝试使用JTextPane呈现HTML和纯文本。 Basically the actual content is hosted on remote server, this content can contain some degree of HTML tags or none at all. 基本上,实际内容托管在远程服务器上,此内容可以包含一定程度的HTML标记,也可以根本不包含。 In my JTextPane I have it defined as shown: 在我的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);

And then using HyperlinkListener wants to be able to render the content when the link is clicked. 然后,使用HyperlinkListener希望能够在单击链接时呈现内容。 I am doing so using the syntax below; 我正在使用下面的语法;

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

Unfurtunately when we click on the link to render the content, we end up with the exception: 不幸的是,当我们单击链接以呈现内容时,我们最终遇到了一个例外:

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)

This looks like when the content has no HTML tag available. 当内容没有可用的HTML标记时,这看起来像。 Can someone help us resolve this issue so we can render both plain text and HTML. 有人可以帮助我们解决此问题,以便我们同时渲染纯文本和HTML。

Thanks in advance. 提前致谢。

EDITED. 已编辑。

It sounds like what you're saying is that since you want to support both HTML and plain-text from your JTextPane as input, then not receiving a URL isn't really considered a problem. 听起来您在说的是,由于您希望同时支持JTextPane的HTML和纯文本作为输入,因此不接收URL并不是真正的问题。 In that case, you should consider logging/eating the exception: 在这种情况下,您应该考虑记录/饮食异常:

jText.addHyperlinkListener(new HyperlinkListener()
{
  public void hyperlinkUpdate(final HyperlinkEvent he)
  {
    try {
      //Render the page
      setPage(he.getURL()); //Error on this line
    } catch (IllegalArgumentException e) {
      // either log the argument here, or just eat it and do nothing with it.  logger.error() recommended
    }
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM