Reputation: 431
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
Upvotes: 3
Views: 2962
Reputation: 301
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.
Upvotes: 0
Reputation: 111
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);
}
Upvotes: 0
Reputation: 33
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.
Upvotes: 3