tags:

views:

73

answers:

3

In my app I have some longish labels, on a chinese os the labels overflow and push out other components also. This has only been observed on a chinese os. How can I detect and handle overflowing components?

+1  A: 

JComponent has a setMaximumSize method that could help you. Depending on the LayoutManager you are using, the results may be different. For setting constraints like this at Layout level, check the SpringLayout.

Daniel H.
A: 

I am guessing that this is a problem for some, depending on the window-size and language.

Maybe not what you want, but you can check the size of the text like this:

// get metrics from the graphics
FontMetrics metrics = graphics.getFontMetrics(font);
// get the height of a line of text in this font and render context
int hgt = metrics.getHeight();
// get the advance of my text in this font and render context
int adv = metrics.stringWidth(text);

If the text is longer than a limit (you have to pull out of a hat,) you can truncate or perhaps even abbreviate it - if it is known, or possibly replace it with an icon and use a tooltip to show the full text.

KarlP
This is best solution for my purposes. There is one label overflowing, however there is also a titledborder overflowing. So using myComponent.getFontMetrics(font) and inserting tooltip if over certain length works well, though not ideal.
A: 

You can make a JTextArea behave like a label with a bit of extra code, but it will wrap the text over lines as necessary instead of abbreviating with an ellipsis (...). Design your UI with wrapping in mind.

JTextArea label = new JTextArea();
label.setText( text );
label.setWrapStyleWord( true );
label.setLineWrap( true );
label.setEnabled( false );
label.setOpaque( false );
banjollity