Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes Enforcing Minimum Size in Frames Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Enforcing Minimum Size in Frames" Watch "Enforcing Minimum Size in Frames" New topic

Enforcing Minimum Size in Frames

Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Well this is pretty simple, I'm wondering how I ensure that my JFrame or JInternalFrame has a certain minimum size. I'm using GridBagLayout and at a certain point the components inside it, labels, text fields, etc. stop scaling and instead start moving becoming a jumbled overlapping mess of components. Basically, rather than trying to make it so that these components have no minimum size I'd rather just make it impossible for the frame to become small enough for that to happen, but I'm not sure how do this and I haven't found anything that shows me after searching. I know that I can use a ComponentListener and when it gets too small directly change the size to be big enough but that doesn't seem to work while dragging and I'm sure that can't be the best way.
So, what's the proper way of handling this as I tried setting a minimum size for the frame and that didn't seem to work. The other thing I want to know is how I go about finding out what this minimum size should be since it could vary depending on the look & feel, font, etc.
Any help or direction is appreciated.
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
You could put a panel inside a scrollpane and then set minimum, preferred and maximum sizes on the panel so that it is always well laid out. Users will just see less of the panel if they decrease the frame size, but can still scroll the complete panel.

The nice thing about Standards is that there are so many to choose from!
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
That doesn't really address the problem. Besides if I use a scroll pane the components will use their preferred sizes instead of resizing forcing the user to scroll around when they shouldn't have to.
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
I ran across this situation and after searching came up with the following. It ain't exactly pretty, but it does work.

Obviously you could get the min values (MIN_WIDTH and MIN_HEIGHT) some other way. You listen for resize events on the frame and if the user tries to go below some minimum threshhold, you increase to the mininum height or width or both.
It's easy and it works.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
As I mentioned I knew I could use a component listener but that doesn't seem to work during drag and I figured there MUST be a better way. There isn't some minimum size that I can set somewhere or something?
I agree. Here's the link:
subject: Enforcing Minimum Size in Frames
Similar Threads
Custom Layout Manager
component layout not wrapping inside Panel
GUI: Should we make the window size resizable?
Layout Managers
Setting frame minimum size