4

I have a very simple JLabel with a quite long text.
The parent layout is a GridBagLayout.
I want the width of the containing Frame not to exceed a certain value, say 160 pixel.
So I set the text as "html", so as the JLabel is able to wrap the text into multiple lines.
Now I just want to "fix" the JLabel width. But I haven't found a way.
The maximum size of the Jframe, the JLabel is not checked by layout manager, so i don't know if there's a "plain" solution.

The very simple example (just fix imports to run) shows the situation.

public class SSCE extends JFrame {

    public static void main(String [] args) {
        new SSCE().setVisible(true);
    }

    public SSCE() {

        setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.fill = GridBagConstraints.BOTH;

        JLabel label = new JLabel("<html>one two three four five six seven eight nine ten eleven twelve thirteen");
        add(label, gbc);

        gbc.gridy=1;
        JLabel label2 = new JLabel("other content");
        add(label2, gbc);

        pack();

    }

}
Nate W.
  • 9,141
  • 6
  • 43
  • 65
AgostinoX
  • 7,477
  • 20
  • 77
  • 137

3 Answers3

5

Try specifying the width in the HTML, like this:

String html = 
    "<html><body width='150px'>" +
    "Some content that will span 150 pixels and then line-wrap as necessary" +
    "</body></html>";
new JLabel( html );

Note that I've specified pixels (150px) in this example, but HTML body width can be specified other ways.

Nate W.
  • 9,141
  • 6
  • 43
  • 65
  • 1
    @AndrewThompson: Huh? That's just some arbitrary number, the point is that you can specify width this way. In fact, you're the person who showed me [here](http://stackoverflow.com/questions/4083322/how-can-i-create-a-jtextarea-with-a-specified-width-and-the-smallest-possible-he/4093402#4093402) :) – Nate W. Nov 16 '11 at 02:05
  • @AndrewThompson: Ha :D. I saw your edit, I didn't know about pixels being the default. I'm willing to bet that a relative size (`150%`) wouldn't work here as there's no parent HTML component, right? – Nate W. Nov 16 '11 at 02:15
  • *"..right?"* Your JRE is as good as mine. What happened when you tried it? – Andrew Thompson Nov 16 '11 at 02:46
2

Maybe try to use prefered and maximum sizes for JLabel ?

Sergii Zagriichuk
  • 5,389
  • 5
  • 28
  • 45
  • label.setMaximumSize(new Dimension(80,Integer.MAX_VALUE)); tried, of course. It doesn't work. In the javadoc, GridBagLayout is said to consider max value. Perhaps it evaluate max value in other circumstances, for example when it redistribute extra space... i don't know. It's rather complex for me to comprehend all the details of this layout manager. – AgostinoX Nov 15 '11 at 23:42
  • I think Maximum and Prefered sizes should be equal to your maximum value , in your case is 160 . – Sergii Zagriichuk Nov 16 '11 at 10:21
2

There was a similar question: "make a JLabel wrap it's text by setting a max width"

And this answer seems to be the right one:JLabel Max Width Answer

Community
  • 1
  • 1
Guido Anselmi
  • 3,872
  • 8
  • 35
  • 58