Java HTMLDocument (insertAfterEnd, insertAfterStart, insertBeforeEnd, insertBeforeStart) not working?
Asked Answered
U

1

5

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.

Unintentional answered 21/11, 2011 at 15:39 Comment(0)
F
7
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.

Fyke answered 21/11, 2011 at 17:12 Comment(4)
+1 setText() in fact could recreate the document instance. Check whether getDocument() returns the same one. If not use the latest doc ument instance.Kenna
Thanks to to both of you. I understand also your advice that JTextPane is more "high level" than JEditorPane. I say this because with your explanations I removed the "jep.setDocument(doc)" and I added "doc = (HTMLDocument)jep.getDocument();" AFTER the setText and everything work fine. Thank you very much. However I need a little further information. I need a very very fast application, that's why I used JEditorPane instead of JTextPane, because as far as I understand, JEditorPane has less "overhead" that JTextPane. Do you agree with that or shall I switch to JTextPane (more "high level") ?Unintentional
(Sorry for late response.) No experience. I do not find the source code very well structured. Though one may not complain with such complexity from the beginning of Java: editing of HTML and more.Fyke
Thank you very much. This post has been a time saver. For the past three days I was struggling with the same problem. I was re-setting the document model via setText (), but was having a stale reference to the document via an earlier call to getDocument (). Removing the getDocument () and replacing it with getStyledDocument () after setText () did the trick!Daedal

© 2022 - 2024 — McMap. All rights reserved.