Search code examples
javahtmlswingjeditorpane

Add HTML in JEditorPane in Java Swing


I would like to add HTML in my JEditorPane but the text is not displayed correctly. Also, when the text's height is greater that the editor's height, the the cursor goes to the last line of the scroll Pane.

My code is as follows:

    JPanel JPInfo = new JPanel(new BorderLayout());
    JPInfo.setBorder(BorderFactory.createTitledBorder("Information"));
    editorPaneInfo = new JEditorPane();
    editorPaneInfo.setEditable(false);
    editorPaneInfo.setText("<html><p style=\"color:green\"> Test Test </p></html>");
    JScrollPane editorScrollPaneInfo = new JScrollPane(editorPaneInfo);
    editorScrollPaneInfo.setVerticalScrollBarPolicy(
                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    JPInfo.add(editorScrollPaneInfo,BorderLayout.CENTER);

SOLVED: I added the following line before setText

 editorPaneInfo.setContentType("text/html");

Solution

  • My problem was solved:

    I added the following line before .setText()

    editorPaneInfo.setContentType("text/html");