Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enforcing Minimum Size in Frames

 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic