Question

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?

Was it helpful?

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

OTHER TIPS

Probably not the best way, but you might try this:

pn.setText(pn.getText + "your text to add here");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top