HTMLEditorKit insertHTML adds a newline tag to the string

java convert string to html document
jsoup
java html api
jsoup build html
jsoup documentation
jsoup example
groovy jsoup
jsoup parse
HTMLEditorKit.insertHTML(doc, doc.getLength(), "Test", 0, 0, null);

The result:

"Test
"

-Added '\n'

HTMLEditorKit.insertHTML(doc, doc.getLength(), "Test", 0, 0, HTML.Tag.B);

The result:

"Test</b>"

How to make text with nothing added at the end?

I use JTextPane. Packing a method named "append(string)" using hTMLEditorKit.insertHTML method to append text.

but when I append some strings like "123"; "456"; "789";

I have never append string like " "

the component can not display that I wanted:

123456789

It will display the text like in JTextPane:

123
456
789

First of all, I don't know if you have arleady solved your problem, considering you asked this question three months ago hehe. But I had this problem as well and thought I'd post how I solved it.

This might be an ugly solution, but this is how I did it:

HTMLEditorKit.insertHTML(doc, doc.getLength(), "<span>Test</span>", 0, 0, HTML.Tag.SPAN);

Likewise, the following consecutive insertHTML calls would print the numbers in the way you wanted:

HTMLEditorKit.insertHTML(doc, doc.getLength(), "<span>123</span>", 0, 0, HTML.Tag.SPAN);
HTMLEditorKit.insertHTML(doc, doc.getLength(), "<span>456</span>", 0, 0, HTML.Tag.SPAN);
HTMLEditorKit.insertHTML(doc, doc.getLength(), "<span>789</span>", 0, 0, HTML.Tag.SPAN);

I'm not sure why it's not enough to just include the span tag in the string, but you also have to pass the HTML.Tag.SPAN with the function call (which is what took so long for me to figure out). Perhaps it overrides the default behaviour of adding newlines if no Tag object has been specified.

Only problem with this approach is you get all these span tags cluttering the html document which is, at least to me, undesirable.

If there's any other (more graceful) way to manipulate the HTMLEditorKit to not automatically add newlines I would love to know about it.

insertHTML does not work properly for   tag, I'm using a JEditorPane with HTMLEditorKit and HTMLDocument and want to insert a non-breaking space into public static void main(String args[]) { try &#​160; 345. I can live with it converting the nbsp; to  , but the newlines are a real Scott Violet (Inactive) added a comment - 2000-08-21 17:00. InsertHTMLTextAction can be used to insert an arbitrary string of HTML into an existing HTML document. At least two HTML.Tags need to be supplied. The first Tag, parentTag, identifies the parent in the document to add the elements to. The second tag, addTag, identifies the first tag that should be added to the document as seen in the HTML string.


I found :

You just need to replace " " by the unicode for it. You can use a documentFilter or intercept the space keyStroke :

protected class Filtre extends DocumentFilter implements Serializable {

    public Filtre() {
    }

    @Override
    public void insertString(DocumentFilter.FilterBypass fb, int offset, String str, AttributeSet attr) throws BadLocationException {
        replace(fb, offset, 0, str, attr);
    }

    @Override
    public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String str, AttributeSet attrs) throws BadLocationException {
        String result = str.replaceAll(" ", "\u00A0");
        fb.replace(offset, length, result, attrs);
    }

javax.swing.text.html.HTMLEditorKit.insertHTML java code , insertHTML(doc, doc.getLength(), "<b>hello", 0, 0, HTML.Tag.B); kit. getCaretPosition(); HTMLEditorKit e = new HTMLEditorKit(); try { String Path  An EditorKit has the ability to be read and save documents. It is generally the most pleasing to users if there is no loss of data between the two operation. The policy of the HTMLEditorKit will be to store things not recognized or not necessarily visible so they can be subsequently written out.


I think there was a simple solution to this problem, using:

doc.insertString(doc.getLength(), "Test", null);

when you want to add plain text and HTMLEditorKit.insertHTML when html.

HTMLDocument (Java Platform SE 8 ), HTMLReader , which implements the HTMLEditorKit. The following methods can be used to insert HTML content into an existing document. A convenient shortcut for locating elements is the method getElement(String) ; returns an Consider, if you were to replace a character element that contained a newline with  321: */ 322: protected HTML.Tag addTag; 323: 324: /** 325: * Alternate tag in HTML to start adding tags from if parentTag is 326: * not found and alternateParentTag is not found. 327: */ 328: protected HTML.Tag alternateAddTag; 329: 330: /** 331: * Alternate tag to check if parentTag is not found.


nl2br - Manual, PHP 5, PHP 7). nl2br — Inserts HTML line breaks before all newlines in a string 5.3.0, Added the optional is_xhtml parameter. Before this Rather, it inserts a <br> tag before each newline, but it still preserves the newlines themselves! The HTML <p> element defines a paragraph: Note: Browsers automatically add some white space (a margin) before and after a paragraph. You cannot be sure how HTML will be displayed. Large or small screens, and resized windows will create different results. With HTML, you cannot change the output by adding extra spaces or extra lines in your HTML


HTMLDocument.java, If a <BASE> * tag is encountered, the base will become the URL specified insertTag the first tag to start inserting into document */ public HTMLEditorKit. private void updateFrameSet(Element element, String url) { try { int startOffset = element. charAt(0) == NEWLINE[0]) { offset--; } insertHTML(elem, offset, htmlText,  InsertHTMLTextAction can be used to insert an arbitrary string of HTML into an existing HTML document. At least two HTML.Tags need to be supplied. The first Tag, parentTag, identifies the parent in the document to add the elements to. The second tag, addTag, identifies the first tag that should be added to the document as seen in the HTML string.


Java Answers Forum, Tag.P,HTML.Tag.UL); private HTMLEditorKit.InsertHTMLTextAction bulletAction JMenuItem nextSizeItem = new JMenuItem(String. and am having trouble figuring out how to allow someone to add a hypertext link into the text of the editor​. insertHTML without it automatically inserting a new Paragraph. The following are top voted examples for showing how to use javax.swing.text.html.HTMLEditorKit.These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.