How to set the line spacing in a JtextPane?

南笙酒味 提交于 2019-12-29 08:15:35

问题


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?


回答1:


When you call textPane.setParagraphAttributes(aSet, false); it tries to apply line spacing to selection but nothing is selected

Call it another way

document.setParagraphAttributes(0, document.getLength(), attr, replace);



回答2:


To style JTextPane you could use Stylesheets: Look for HtmlEditorKit#setStyleSheet

StyleSheet sh = editorKit.getStyleSheet();
sh.addRule("body {line-height: 50px}");



回答3:


I was struggling with this problem and then in the API of the method public void setParagraphAttributes(AttributeSet attr, boolean replace), I found this:

If there is a selection, the attributes are applied to the paragraphs that intersect the selection. If there is no selection, the attributes are applied to the paragraph at the current caret position.

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 this JTextPane 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:

/**
 * Select all the text of a <code>JTextPane</code> first and then set the line spacing.
 * @param the <code>JTextPane</code> to apply the change
 * @param factor the factor of line spacing. For example, <code>1.0f</code>.
 * @param replace whether the new <code>AttributeSet</code> should replace the old set. If set to <code>false</code>, will merge with the old one.
 */
private void changeLineSpacing(JTextPane pane, float factor, boolean replace) {
    pane.selectAll();
    MutableAttributeSet set = new SimpleAttributeSet(pane.getParagraphAttributes());
    StyleConstants.setLineSpacing(set, factor);
    txtAtributosImpresora.setParagraphAttributes(set, replace);
}

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 a JScrollPane 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:

pane.setCaretPosition(0); //scroll to the top at last.

P.S.: To set the paragraph margin, we have:

textPane.setMargin(new Insets(10, 5, 10, 5)); //top, left, bottom, right


来源:https://stackoverflow.com/questions/18204675/how-to-set-the-line-spacing-in-a-jtextpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!