I am trying to set the width of a JLabel using an HTML div tag.
Consider the following code:
import javax.swing.*;
public class Xyzzy extends JFrame{
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
Xyzzy frame = new Xyzzy();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.PAGE_AXIS));
String s = "x ";
for (int i=0; i<200; ++i)
s += "x ";
JLabel jl = new JLabel("<html><div style=\"width: 300px;\">" + s + "</div></html>");
frame.add(jl);
frame.setSize(600, 600);
frame.setVisible(true);
}
});
}
}
I would have expected the JLabel to be 300 pixels wide, but in reality it is about 390 pixels wide. If I change the width specification to 200px, the resulting label is about 260 pixels wide.
What am I doing wrong?