JLabel Width Independent from Text Length
I have a JLabel in a horizontally re-sizable JPanel. The JLabel auto re-sizes its width to fit the JPanel. If I insert a long line of text (such as "aaaaaaaaaaaaaaaaaaaaaaaa") the JLabel doesn't truncate the text. Instead, the width re-sizes to开发者_JAVA百科 fit the text, causing also an ugly re-sizing of the JPanel.
Instead, I want my text to be truncated with an elipse (...). The JLabel width must not inherit from the text's length but only from the JPanel width.Try following:
final JLabel label = ...
...
label.setText("prototype text to define size");
final Dimension size = label.getPreferredSize();
label.setMinimumSize(size);
label.setPreferredSize(size);
...
label.setText(...);
Use a different layout or set a max size on the JLabel.
You need to disable 'Horizontal Resizable' when you define a max- and preferedsize
精彩评论