JLabel width independent of text length

I have JLabel in a horizontally multi-valued JPanel. The JLabel machine changes width according to JPanel. If I insert a long line of text (for example, "aaaaaaaaaaaaaaaa"), JLabel does not truncate the text. Instead, the width of the dimensions changes to fit the text, causing an ugly resizing of the JPanel as well. Instead, I want my text to be truncated using elipse (...). The width of the JLabel should not be inherited from the length of the text, but only from the width of the JPanel.

+4
source share
3 answers

Try the following:

final JLabel label = ... ... label.setText("prototype text to define size"); final Dimension size = label.getPreferredSize(); label.setMinimumSize(size); label.setPreferredSize(size); ... label.setText(...); 
+3
source

Use a different layout or set the maximum size to JLabel.

0
source

You need to disable β€œHorizontal resizing” when you define max- and preferisize

0
source

Source: https://habr.com/ru/post/1335666/


All Articles