Pregunta

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?

¿Fue útil?

Solución

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

Otros consejos

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

pn.setText(pn.getText + "your text to add here");
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top