Вопрос

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?

Это было полезно?

Решение

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

Другие советы

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

pn.setText(pn.getText + "your text to add here");
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top