0

In a Layout Manager when I use setPreferedSize it increased the size of JLabel but show only one word like (JLabel = Name) after using setPreferedSize JLabel becomes like N.... But setFont works correctly..

Can anyone tell the exact difference between the behavior of setFont and setPreferredSize?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ahsan Rafiq
  • 111
  • 2
  • 8

1 Answers1

1

But setFont works correctly..

Exactly, and that is the only method you should use. When you use the setFont() method, the component is responsible for determining the preferred size. The component will take into account information like the font, text, border to determine it's preferred size.

When you invoke setPreferredSize(), you are telling the component that you know better, which you don't because you are just making a guess. If your guess is too small you see the "..." because there is not enough space to paint the entire text.

Don't use setPreferredSize()!

camickr
  • 321,443
  • 19
  • 166
  • 288
  • See also [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Jan 08 '15 at 02:13