9

Possible Duplicate:
setMaximumSize not working in java

I've having trouble with my JFrame subclass. I need to set a Maximum height.

But setMaximumSize does not work. It seems that's a bug in java (setMinimumSize works). How could prevent a JFrame from getting higher than a given height ?

Community
  • 1
  • 1
Matthieu Riegler
  • 19,274
  • 14
  • 81
  • 118

2 Answers2

6

In my experience, setMinimumSize and setMaximumSize are not reliable - they may work in some situations, but often not in others

The best solution is to use a ComponentListener, implement componentResized, and enforce the min / max size that way

ControlAltDel
  • 32,042
  • 9
  • 48
  • 75
  • 2
    +1 See also [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing](http://stackoverflow.com/q/7229226/230513)? – trashgod Apr 15 '12 at 02:25
  • Can you elaborate on this? How would you enforce the min/max size by catching resize events? AFAIK these events are fired as notifications and wouldn't be helpful in enforcing size limits. – Lieuwe May 10 '16 at 08:37
5

Use this instead/as well:

setPreferredSize(new Dimension(width, height));

In my experience setPreferredSize(Dimension) takes preference and always works.

I usually use all of them anyway: setMinimumSize(), setMaximumSize(), setPreferredSize() and then setSize() as well

Ozzy
  • 8,027
  • 7
  • 51
  • 93