Tuesday, April 05, 2005

JFrame setMinimumSize() and setMaximumSize() issue

I have been playing around with Swing tonight using JDK 1.5.0_02. I have found an issue where the setMinimumSize and setMaximumSize dimensions are not enforced on a JFrame. I wanted to keep a dialog from being resized smaller than a minimum bounds or larger than a maximum bounds. To do this, I originally used the following code in my application:

frame.setMinimumSize( new Dimension( 250, 200 ) ); frame.setMaximumSize( new Dimension( 325, 275 ) );
Unfortunately, as I mentioned above, this does not prevent you from resizing the frame smaller or larger than the specified sizes. Of course, you could just decide to not allow any resizing of the frame. To do that, you could use this line of code in your application:

frame.setResizable( false );
However, if you want to allow the user to resize a dialog, then this is not a viable alternative. I decided to create my own class derived from JFrame. This class takes a title as well as minimum and maximum dimensions. My constructor for the class looks like this:

private Dimension minimumSize;
private Dimension maximumSize;

public MyJFrame( String title, Dimension minimumSize,
  Dimension maximumSize ) throws HeadlessException
{
  super( title );
  this.minimumSize = minimumSize;
  this.maximumSize = maximumSize;
  initialize();
}
The initialize(): call does nothing more than make a call to addComponentListener on the frame. In this call, I create an anonymous ComponentAdapter class which overrides the componentResized method to handle the constraints on the frame. The code looks like this:

private void initialize() {
    this.addComponentListener( new ComponentAdapter() {
        @Override
        public void componentResized( ComponentEvent e ) {
            MyJFrame frame = (MyJFrame) e.getSource();
            int width = frame.getWidth();
            int height = frame.getHeight();
            boolean resize = false;
            if ( width < minimumSize.getSize().width ) {
                width = minimumSize.getSize().width;
                resize = true;
            }
            else if ( width > maximumSize.getSize().width ) {
                width = maximumSize.getSize().width;
                resize = true;
            }
            if ( height < minimumSize.getSize().height ) {
                height = minimumSize.getSize().height;
                resize = true;
            }
            else if (height > maximumSize.getSize().height ) {
                height = maximumSize.getSize().height;
                resize = true;
            }
            if ( resize ) {
                frame.setSize( width, height );
            }
        }
    })
}
This code is pretty straight forward. It grabs the current size constraints for the frame that fired the event. It then checks these constraints against the specified minimum and maximum dimensions. If the constraints on the frame are not met, the size of the frame is set explicitly, otherwise, the flow falls through and the frame is resized according to the size that the user has resized it.

There may be better ways to handle this situation. This is what I came up with tonight and it works pretty well. Hopefully future versions of the JDK will enforce the setMinimumSize and setMaximumSize dimensions on the JFrame component.

Technorati Tags: , ,

No comments: