Setting minimum size limit for a window in java swing

64,982

Solution 1

The documentation tells me, that this behavior is platform dependent. Especially, since the following example code works for me as desired in Windows Vista:

import java.awt.Dimension;

import javax.swing.JFrame;

public class JFrameExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Hello World");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setMinimumSize(new Dimension(100, 100));
        frame.setVisible(true);
    }
}

Solution 2

There actually is a way to ensure minimum size on any platform. You need to set the minimum size of the JFrame to the minimum size of its content pane and then you need to write a ComponentAdapter and override componentResized. Then you just use getSize and getMinimum size on your JFrame and substitute width and/or height with the minimum width or height if it is greater. Assuming you are extending JFrame:

this.addComponentListener(new ComponentAdapter(){
        public void componentResized(ComponentEvent e){
            Dimension d=YourJFrame.this.getSize();
            Dimension minD=YourJFrame.this.getMinimumSize();
            if(d.width<minD.width)
                d.width=minD.width;
            if(d.height<minD.height)
                d.height=minD.height;
            YourJFrame.this.setSize(d);
        }
    });
Share:
64,982
shadyabhi
Author by

shadyabhi

Linux Geek, Foodie, Biker!

Updated on August 31, 2020

Comments

  • shadyabhi
    shadyabhi over 3 years

    I have a JFrame which has 3 JPanels in GridBagLayout..

    Now, when I minimize a windows, after a certain limit, the third JPanel tends to disappear. I tried setting minimizing size of JFrame using setMinimumSize(new Dimension(int,int)) but no success. The windows can still be minimized.

    So, I actually want to make a threshhold, that my window cannot be minimized after a certain limit.

    How can I do so?

    Code:-

    import java.awt.Dimension;
    
    import javax.swing.JFrame;
    
    public class JFrameExample {
        public static void main(String[] args) {
            JFrame frame = new JFrame("Hello World");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setMinimumSize(new Dimension(400, 400));
            frame.setVisible(true);
        }
    }
    

    Also:

    shadyabhi@shadyabhi-desktop:~/java$ java --showversion
    java version "1.5.0"
    gij (GNU libgcj) version 4.4.1
    
    Copyright (C) 2007 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    Usage: gij [OPTION] ... CLASS [ARGS] ...
              to invoke CLASS.main, or
           gij -jar [OPTION] ... JARFILE [ARGS] ...
              to execute a jar file
    Try `gij --help' for more information.
    shadyabhi@shadyabhi-desktop:~/java$
    

    Gives me output like

    alt text

    **UPDATE: ** The same when run though Netbeans IDE gives expected output.. When I run through "java JFrameExample" compiler, I am facing issues.. Now, what that means??