I am in the midst of a project (software support for a logic textbook) that requires a simple HTML editor. I cannot use the JavaFX HTMLEditor because it does not support superscript / subscript, and because it forces its toolbars. So I have been using JTextPane with a SwingNode. This has gone well up to line spacing. The code below is a stripped-down version of my problem: Each of the commented lines works to format the paragraph. But the uncommented one to set line spacing does not. As exhibited by the print statement, the paragraph attribute does get set. The problem is that the spacing does not render (and in the underlying document there is no change to the HTML paragraph tag, as there is for the other formatting commands).
public class LineSpaceTest extends JFrame {
JTextPane pane = new JTextPane();
LineSpaceTest() {
pane.setContentType("text/html");
getContentPane().add((pane));
MutableAttributeSet mutableAttributeSet = new SimpleAttributeSet();
// StyleConstants.setLeftIndent(mutableAttributeSet, 36f);
// StyleConstants.setRightIndent(mutableAttributeSet, 35.0f);
// StyleConstants.setSpaceAbove(mutableAttributeSet, 20.0f);
// StyleConstants.setSpaceBelow(mutableAttributeSet, 20.0f);
// StyleConstants.setFirstLineIndent(mutableAttributeSet, 36.0f);
// StyleConstants.setAlignment(mutableAttributeSet, StyleConstants.ALIGN_CENTER);
StyleConstants.setLineSpacing(mutableAttributeSet, 2f);
HTMLDocument doc = (HTMLDocument) pane.getDocument();
doc.setParagraphAttributes(0, doc.getLength(), mutableAttributeSet, false);
System.out.println(StyleConstants.getLineSpacing(doc.getParagraphElement(0).getAttributes()));
this.setMinimumSize(new Dimension(500, 300));
setVisible(true);
}
public static void main(String[] args) {
new LineSpaceTest();
}
}
I have had the problem in both Java 8 and Java 14. There is a related question here How to set the line spacing in a JtextPane?. And there appears to have been a related bug fixed in version 1.4.0_02 https://bugs.openjdk.java.net/browse/JDK-4242645. So far as I can tell the proposed solutions do not solve, but only raise, the question why it does not work for me. Of course, I am new to Java (and this is my first StackOverflow post), so I could be missing something simple. . .