Java HTMLDocument(insertAfterEnd、insertAfterStart、insertBeforeEnd、insertBeforeStart)不起作用?

发布于 2024-12-17 06:47:40 字数 2263 浏览 1 评论 0原文

我有一个 JEditorPane,它显示以编程方式(在运行时)生成的 HTML。 到目前为止,当我添加“行”时,我在字符串缓冲区中重新创建整个 HTML 文本,然后将其传递给 JEditorPane.setText 方法。

现在创建的 HTML 已经变得相当大(可达几 MB),我只需在末尾添加新行,而不是重新生成所有 HTML 文本。

我尝试在末尾附加的原因是为了避免 Swing(或套件?)必须再次渲染/解析整个文本。因为即使 HTML 生成不是在 EDT 中执行而是在另一个 swingworker 线程中执行,“渲染”也需要很长时间。或者最好是有一个进度条显示渲染的进度,这是不可能的(是吗?)。

所以我的想法是简单地附加在最后,但如果您有更好的想法,欢迎!

由于我的文本采用 HTML 表格格式,因此我想将新文本附加到该表格的末尾。为此,我尝试使用 HTMLDocumentinsertBeforeEnd,但即使我尝试了很多解决方案,也无法使其正常工作。请注意,我只有“table”标签。

这是我的代码的一部分

JEditorPane jep = new JEditorPane();
HTMLEditorKit kit = new HTMLEditorKit();
HTMLDocument doc = new HTMLDocument();

jep.setEditorKit(kit);
jep.setDocument(doc);

//setting dummy text within a HTML table
jep.setText("<table><tr><td>A line of text</td></tr><tr><td>Another line of text</td></tr></table>");

现在在此表的末尾附加一些文本

//getting the Table Element
Element e = doc.getElement(doc.getDefaultRootElement(), StyleConstants.NameAttribute, HTML.Tag.TABLE);

请注意,该元素似乎已正确找到,因为 System.out.println(e.getName()) 给出了“table “

现在

//inserting text at the end of the table
try {
        doc.insertBeforeEnd(e, "<tr><td>A New Line</td></tr>");
    } catch (BadLocationException ex) {
        System.out.println(ex);
    } catch (IOException ex) {
        System.out.println(ex);
    }

给我带来了一个例外:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: No HTMLEditorKit.Parser
at javax.swing.text.html.HTMLDocument.verifyParser(HTMLDocument.java:1500)
at javax.swing.text.html.HTMLDocument.insertBeforeEnd(HTMLDocument.java:1248)
...

编辑

我已经就这个问题的后续问题提出了一个新问题,这里是链接:

https://stackoverflow.com/questions/9659209/jeditorpane-htmldocument- Different-rendering-how-为什么

尽管 @JoopEggen 的答案一切正常,但字体渲染并不相同,我不明白为什么。因为在我看来,这是一个与此处发布的问题不同的问题,我在另一个问题中提出了这个问题(上面给出的链接)。但就我而言,这在某种程度上是这件事的后续。

由于有些人可能会遇到同样的问题,我设置此编辑以将您指向相应的线程。

I have a JEditorPane that displays HTML that is generated programmatically (at runtime).
Up to now when I was adding a "line" I was re-creating the whole HTML text in a string buffer and then passing it to JEditorPane.setText method.

Now the HTML created has become quite large (can reach up to a few MB) and I would simply add my new line at the end instead of re-generating all the HTML text.

The reason I try to append at the end is to avoid Swing (or the kit ?) having to render/parse the whole text again. Because even though the HTML generation is not performed in the EDT but in another swingworker thread, the "rendering" takes ages. Or the best would be to have a progress bar displaying the progression of the rendering, which is not possible (is it ?).

So my idea is to simply append at the end, but if you have a better idea, it is welcome !

As my text is formatted in an HTML table, I would like to append my new text at the end of this table. For this I tried to use the insertBeforeEnd of the HTMLDocument but I don't manage to have it working even though I tried plenty of solutions. Notice that I have only "table" tag.

Here is some part of my code

JEditorPane jep = new JEditorPane();
HTMLEditorKit kit = new HTMLEditorKit();
HTMLDocument doc = new HTMLDocument();

jep.setEditorKit(kit);
jep.setDocument(doc);

//setting dummy text within a HTML table
jep.setText("<table><tr><td>A line of text</td></tr><tr><td>Another line of text</td></tr></table>");

Now for appening some text at the end of this table

//getting the Table Element
Element e = doc.getElement(doc.getDefaultRootElement(), StyleConstants.NameAttribute, HTML.Tag.TABLE);

Note that the element seems to be found correctly as System.out.println(e.getName()) gives "table"

Now

//inserting text at the end of the table
try {
        doc.insertBeforeEnd(e, "<tr><td>A New Line</td></tr>");
    } catch (BadLocationException ex) {
        System.out.println(ex);
    } catch (IOException ex) {
        System.out.println(ex);
    }

Raises me an exception:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: No HTMLEditorKit.Parser
at javax.swing.text.html.HTMLDocument.verifyParser(HTMLDocument.java:1500)
at javax.swing.text.html.HTMLDocument.insertBeforeEnd(HTMLDocument.java:1248)
...

EDIT

I've started a new question on the follow up of this one, here is the link :

https://stackoverflow.com/questions/9659209/jeditorpane-htmldocument-different-rendering-how-why

Even though everyhting is working fine with the answer of @JoopEggen the font rendering is not the same and I don't understand why. As it seems to me a different problem that the one posted here I asked it in another question (the link given above). But it is in my case somewhat of a follow up of this one.

As some may face up the same problem I set this EDIT to point you to the corresponding thread.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心不设防 2024-12-24 06:47:40
private HTMLDocument doc;
...
JTextPane jep = jTextPane1;
jep.setContentType("text/html");
jep.setText("<html><table><tr><td>A line of text</td></tr><tr><td>Another line of text</td></tr></table>");
doc = (HTMLDocument)jep.getStyledDocument();

内容类型后跟 setText 将安装 EditorKit 并确定文档。因此,请稍后获取 StyledDocument。 setText("...") 再次确保采用 HTML。 (你可以有一个 JLabel 或 JButton 与 "< html >< b >H< /b >< i >ello< /i >< span style='color: #0ff078' > !!!< /span >"。

JTextPane 比 JEditorPane 级别更高(奇怪的命名)。它提供了 StyledDocument,您可以通过它来执行剩下

的还好。

private HTMLDocument doc;
...
JTextPane jep = jTextPane1;
jep.setContentType("text/html");
jep.setText("<html><table><tr><td>A line of text</td></tr><tr><td>Another line of text</td></tr></table>");
doc = (HTMLDocument)jep.getStyledDocument();

The content type followed by a setText installs the EditorKit and determines the document. For that reason take the StyledDocument afterwards. The setText("...") again ensures that HTML is taken. (You could have a JLabel or JButton with "< html >< b >H< /b >< i >ello< /i >< span style='color: #0ff078' >!!!< /span >".

JTextPane is more high level as JEditorPane (strange naming). It provides StyledDocument by which you could do more.

The rest is okay.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文