Michał Dębski
Michał Dębski

Reputation: 537

Set maximum size of JFrame

I'm starting my journey with Java and I'm playing with swing. I made some simple JFrame with JScrollPane inside and I'm looking for a way to restrict my frame to preset minimum and maximum size. I've searched the net about setMaximum and setMinimum and how wrong they are, but I haven found nothing(Except some weird ComponentListener). However setMinimum is working fine(at least for now) but i can't get setMaximum to work. How can I set such constraints?

EDIT:
What I mean by "Except some weird ComponentListener" is that it lets me resize frame, then just resize it back. What I want to achive is invisible bound, that prevents frame from going more.

EDIT2:
It seems that this problem concerns my OS(which is OSX), ComponentListener works fine on Windows. But on Mac, when I start dragging the window out, ComponentMoved is called and only when I release mouse button ComponentResized is called. When I copy code from Resized to Moved, first, window resize itself over MaximumSize for fraction of second, then apropiet code is called and everything is fine, but this flicker is far from acceptable. I've even overloaded paint(Graphics g) to first reset size and then call super.paint but with same result.

Upvotes: 1

Views: 1282

Answers (1)

user2340612
user2340612

Reputation: 10703

You can add a ComponentListener to your frame and check into componentResized(ComponentEvent e) if the new values for width&height are allowed and, in case they are, you resize the frame through setSize.

Upvotes: 1

Related Questions