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.