Handling overflowing components in a swing

In my application I have long shortcuts, on chinese os overflow of decals and preemption of other components. This was only seen on Chinese os. How can I detect and handle overflowed components?

0


a source to share


3 answers


I guess this is a problem for some, depending on the window size and language.

Maybe not what you want, but you can check the text size 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 the limit (you have to pull the hat out), you can trim or perhaps even shorten it - if known, or perhaps replace it with an icon and use the tooltip to show the full text.

0


a source


JComponent has a setMaximumSize method that can help you. Results may vary depending on the LayoutManager used. For setting constraints like this at the layout level, check out SpringLayout .



+1


a source


You can make the JTextArea behave like a shortcut with a little extra code, but it will wrap the text line by line if needed, rather than an ellipsis abbreviation (...). Build your interface with trickery.

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

      

0


a source







All Articles