3

I have a JLabel with unknown content and I have two things I want to do:

  • I want to set a maximum or perhaps even static width of the label. And if the text is larger than the label it would somply shorten it, like this:

Verylonglabel

becomes

Veryl

Is it a bad idea to use static width on components in a gui? If that is the case, what is the alternative? Please give me advice!

  • When you hover over the label I want a tooltip with the full length string to appear. So in our case, if i hover over the label that says "Veryl", a tooltip displaying "Verylonglabel" would appear. However, it should display a tooltip with the full length string even if it was not shortened.

Help with either of these is greatly appreciated.

So far I've just messed around a bit and tried things like this without sucess. It doesn't seem to care about the size at all.

JLabel label = new JLabel("Verylonglabel");     
label.setSize(15, 5);

Best regards, Goatcat

Goatcat
  • 1,133
  • 2
  • 14
  • 31

3 Answers3

5

The size of your JLabel is determined by the LayoutManager of the parent container. Consult the tutorial for more information.

Note that the JLabel has already the behavior you are looking for

  1. When the text is too long, the text which is cut-off will be replaced by "..." . So in your example, the "Verylonglabel" would be replaced by e.g. "Verylo..."
  2. You can use the setToolTipText method to specify the tooltip, which will be shown when hovering over the JLabel
Robin
  • 36,233
  • 5
  • 47
  • 99
  • 1
    Thank you for you answer, i have a question though: You say "1. When the text is too long, ..", but what decides when it is too long? Where do i set this value? I am now using MigLayout btw. – Goatcat Jun 26 '13 at 10:57
  • @Goatcat your `LayoutManager` decides this. Your `JLabel` decides what its preferred size is based on the text. However, it depends on the `LayoutManager` whether it respects that preferred size or not – Robin Jun 26 '13 at 12:15
2

This is what you need:

JLabel label = new JLabel("Verylonglabel");

// Create tool tip.
label.setToolTipText(label2.getText());

// Set the size of the label
label.setPreferredSize(new Dimension(80,40));// Width, Height
ColinWa
  • 919
  • 1
  • 10
  • 27
  • Some people say that you should avoid `.setPreferredSize()`. What do you have to say on the subject? When is it safe to use, and when should it be avoided? – Goatcat Jun 26 '13 at 11:30
  • 1
    You should generally avoid using setPreferredSize because your GUI component won't change size when the window changes size, like when the user maximizes the window. – Gilbert Le Blanc Jun 26 '13 at 12:37
  • @Robin and Gilbert Le Blanc, thank you both very much for your help! – Goatcat Jun 26 '13 at 12:42
0

setMaximumSize will only be effective if the LayoutManager you use honors components' desired/max sizes. You may want to check out BoxLayout as a LayoutManager. Unlike several other Swing LayoutManagers, BoxLayout honors the size settings of its components.

This still leaves you the question of what size to set as the maximum size. The width will be whatever you are targeting as your fixed value, but the height should be big enough for whatever font is being used. Here is the code I would use to make the height flexible but the width fixed to 50 pixels:

label.setMaximumSize(new Dimension(10000, 50));

Finally, the tooltip will show the full label text with a line like:

label.setToolTipText(labelText);
Josh Britton
  • 83
  • 10