How to set line spacing / height in JLabel in Java Swing?
I have the following JLabel code:
JLabel someJLabel = new JLabel("<html>first line<br>second line</html>"); someJLabel.setFont(new Font("Arial", Font.PLAIN, 16)); What I would like to do is control the height / spacing between two lines.
PS: I also considered using paragraphs instead of broken lines, but this is the same. And I don't know if you can do this in the html tag without using css (you cannot use css inside the html code in JLabel in Java Swing).
This should work, but it is not. color: green works.
content.add(new JLabel("<html><p style=\"line-height: 150%;\">hi<br>world</p></html>"));
I think the line-height is not working. What would you do if you were using CSS, so you might not be able to do this. Here is a nice tool I found that you can use to check if your HTML will work fast.
Check the setStyleSheet (...) method for HTMLEditorKit. I have never used it before, but I believe that it provides basic support.
Otherwise, you can use JTextPane to control line spacing. I think you would use:
StyleConstants.setLineSpacing(...); Then you can change the foreground / background, etc., so that the text panel looks like a label.