4

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?

mKorbel
  • 108,320
  • 17
  • 126
  • 296
oz1cz
  • 4,837
  • 5
  • 29
  • 54
  • I don't know if it's always true, but in your case the size is always 30% bigger than the given width, try using `float size= 300 /1.3f; JLabel jl = new JLabel("
    " + s + "
    ");` And the size will be of 300px. It's a wired workaround and I'm not sure it always work so I just put it in comment.
    – alain.janinm May 21 '12 at 14:58

3 Answers3

5

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

vloo
  • 71
  • 3
  • If HTML in JLabel is outdated, how do I make the text wrap? According to [this link](http://stackoverflow.com/questions/7861724/is-there-some-word-wrap-property-of-jlabel-exist), HTML is the way to go. – oz1cz May 21 '12 at 16:07
  • 1
    @ClausTøndering No HTML in JLabel is not outdated, it's the article and some of the description that are old and incorrect now. – alain.janinm May 21 '12 at 17:55
2

You're setting the width of the text in the HTML, not the width of the JLabel.

Use the setPreferredSize method to set the width of the JLabel.

frame.pack();

Dimension d = label.getSize();
d.width = width;
label.setPreferredSize(d);

Although it's better to let the JLabel size itself to allow the text to fit, as you've seen.

Gilbert Le Blanc
  • 45,374
  • 5
  • 61
  • 107
  • get/setPreferredSize does not work properly with the layout I'm using. Also, I still need the ... tags in order to force the text in the JLabel to wrap. – oz1cz May 21 '12 at 15:59
  • 1
    @Claus Tøndering: You have to get the frame to lay out before you can use the sizes. I fixed an error in my answer. – Gilbert Le Blanc May 21 '12 at 16:22
0

Gilbert, I add your code and end up with this:

import javax.swing.*;
import java.awt.*;

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>" + s + "</html>");

                frame.add(jl);
                frame.pack();

                Dimension d = jl.getSize();
                d.width = 200;
                jl.setPreferredSize(d);

                frame.setSize(600, 600);

                frame.setVisible(true);
            }
        });
    }
}

This simply does not work. I've also tried removing the HTML tags and the frame.setSize(600,600), neither of which produces what I'm looking for: A JLabel that is 200 pixels wide and has adjusted its height.

oz1cz
  • 4,837
  • 5
  • 29
  • 54