javahtmlswingjeditorpanehtmleditorkit

Append text on same line to the JEditorPane as html format


I want to append html content in the JEditorPane, but when I append in this way it inserts a line break automatically at the end of existing text, how to avoid this.

JEditorPane pn = new JEditorPane();
pn.setContentType("text/html");
pn.setText("This is line 1");
...
//after some time
HTMLDocument doc = (HTMLDocument) pn.getDocument();
HTMLEditorKit kit = (HTMLEditorKit) pn.getEditorKit();
kit.insertHTML(doc, doc.getLength(), "<b>Hello</b>", 0, 0, null);
kit.insertHTML(doc, doc.getLength(), "World", 0, 0, null);

It is going to place a linebreak at the end of existing text, everytime insertHTML() is called. Is this a default behaviour? If so how I can handle it?


Solution

  • HTMLDocument has methods

    public void insertAfterStart(Element elem, String htmlText)
    public void insertBeforeEnd(Element elem, String htmlText)
    public void insertBeforeStart(Element elem, String htmlText)
    public void insertAfterEnd(Element elem, String htmlText)
    

    Where you can pass paragraph or character element (leaf) and html to be inserted