tags:

views:

110

answers:

1

I have a JLabel that I need to draw off screen with some Transforms applied to the Graphics object.

I'm trying to make the JLabel as small as it can be without ellipsis depending on its text content.

What I'm trying to do is:

setBounds(0, 0, label.getMinimumSize().width, label.getMinimumSize().height);

Sometimes, this yields ellipsis and sometimes now. It really depends on the transform being applied (usually arbitrary scaling in an animation).

Am I missing something obvious with what I should consider "Minimum Size", am I getting some rounding errors that are rounding down, etc?

Please point me in the right direction.

Thank you

+2  A: 

The label.getMinimumSize() is computed by the UI delegate and has more to do with the layout and its chrome rather than its content. Maybe you can try computing what the size should be based upon the text?

Something like...

JLabel label = new JLabel("xx");
int minWidth = ((int) (label.getFontMetrics(label.getFont()).getStringBounds(label.getText(),label.getGraphics()).getWidth()) + label.getInsets().left + label.getInsets().right);

and similar for the the height.

Mike Katz