javahtmlswingjtextpanejeditorpane

JTextPane/JEditorPane displays HTML <!--comment-->


I am using a Java (21) Swing JEditorPane or JTextPane to display a HTML String. This works, but displays HTML comments p.e. <!-- Hidden Text -->.

JEditorPane htmlpane = new JEditorPane();
        htmlpane.setContentType("text/html");
        htmlpane.setText("<html><table><tr><td>Cell 1</td>"
                + "<td>Cell 2</td></tr><tr><td>Cell 3</td>"
                + "<td>Cell 2 A<!--   Hidden Text --> Cell 2 B</td>"
                + "</tr></table></html>");
        panel.add(htmlpane);

HTML Pane with hidden Text

(Same result for JTextPane)

I want to display HTML with the comments hidden. (In general: display HTML as close to correct as possible in a Swing GUI.)


Solution

  • The behavior you encountered appears to be intentional, for when the JEditorPane is editable.

    See the following jdk bug entry regarding this topic: https://bugs.openjdk.org/browse/JDK-6422138

    There in the comments it is stated:

    JEditorPane is supposed to show everything when editable, so the fact that it shows comments is not a bug.

    And also:

    Turn off the editable property to have HTML page rendered normally (like in a browser).

    Your options now are: