First of all, i set the JTextPane like this:
HTMLEditorKit editorKit = new HTMLEditorKit();
HTMLDocument document = (HTMLDocument) editorKit.createDefaultDocument();
JTextPane textPane = new JTextPane();
textPane.setContentType("text/html");
textPane.setDocument(document);
and i want to set the line spacing in the JtextPane , this is my idea,but it can't work:
SimpleAttributeSet aSet = new SimpleAttributeSet();
StyleConstants.setLineSpacing(aSet, 50);
textPane.setParagraphAttributes(aSet, false);
i was wrong?
I was struggling with this problem and then in the API of the method
public void setParagraphAttributes(AttributeSet attr, boolean replace)
, I found this:So the approach of OP will work, but you must apply
textPane.selectAll()
before setting the line spacing. You only have to do it once, and all the text appended to thisJTextPane
will have the same line space, even though you may have no text in the pane when you set the line spacing. I do it when it's instantiated.Thus the code working for me is:
Note: it will replace the current line spacing with factor*(line height of text), not
factor * original line spacing. Strange enough.If the
JTextPane
is in aJScrollPane
and the length of text is too long, it will scroll to the very bottom. Usually we want to see the top part. To reset the position of the scroll, at last you can add:P.S.: To set the paragraph margin, we have:
To style JTextPane you could use Stylesheets: Look for HtmlEditorKit#setStyleSheet
When you call
textPane.setParagraphAttributes(aSet, false);
it tries to apply line spacing to selection but nothing is selectedCall it another way