JLabel with HTML does not set width properly
html, java, jlabel, resize, swing
Solution
This html code is too complicated for JLabel (support only part of HTML specification) http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JLabel.html Size of components depends on Layout. I prefer MigLayout http://www.miglayout.com simple tutorial
Edit: HTML in JLabel is very outdated
Problem
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?