Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set line spacing/height in a 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'd like to do is be able to control the line height/spacing between the two lines.

PS: I've also looked at using paragraphs instead of breaklines, but it's the same thing. And I don't know if you can do that within an html tag without using css (you can't use css within html code in a JLabel in Java Swing).

like image 300
Stephane Grenier Avatar asked Jun 24 '10 19:06

Stephane Grenier


1 Answers

This should work, but it's not. color: green works though.

content.add(new JLabel("<html><p style=\"line-height: 150%;\">hi<br>world</p></html>"));

I guess line-height doesn't work. That's how you'd do it if you were to use CSS, so maybe you can't do it that way. Here's a nice tool I found which you can use to test if your HTML will work quickly.

like image 64
Dave Avatar answered Sep 27 '22 22:09

Dave