constrained resize

OK, I have a JFrame whose dimensions had to be constrained on resize. Basically, it has to grow equally in width and height (think Checkerboard). In order to do that, I had to override the JFrame’s setBounds. However, I had no idea how to apply this same behavior when teh Frame is maximized. When maximized, the Frame would simply blow up and fit the entire screen. Well, it turns out that the same way setSize has been deprecated, setMaximiumSize – although not deprecated, it’s ignored for Frames. So use setMaximizedBounds.


   Rectangle bounds = getGraphicsConfiguration().getBounds();
   int minOfSizes = (int) Math.min(bounds.getWidth(), bounds.getHeight());
   setMaximizedBounds(new Rectangle(new Dimension(minOfSizes, minOfSizes))); // viola!
This entry was posted in Swing. Bookmark the permalink.

One Response to constrained resize

  1. xrellix says:

    how dare they intro a method i’ve not heard off since the last time i used Swing in 1998.. those bastards..

Leave a Reply

Your email address will not be published. Required fields are marked *