32

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
(source: abhijeet1989 at sites.google.com)

**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??

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
shadyabhi
  • 16,675
  • 26
  • 80
  • 131
  • 1
    What platform are you on, and what version of Java are you using? – Joshua McKinnon May 06 '10 at 15:02
  • do you know the specific update number of your java 6? Type 'java -version' and you should see something like: java version "1.6.0_18", 18 would be the update #. It's possible there is a bug that is only fixed in a later version of java 6 – Joshua McKinnon May 06 '10 at 16:36
  • On top of that, could you edit your post to give us a small example of your code, so you show us the problem. – ablaeul May 06 '10 at 19:06
  • I just copy and pasted your code and ran it. Same issue happened.. I am still having the same problem.. – shadyabhi May 07 '10 at 02:56
  • Issue solved... I was using java 1.5.0.. while netbeans is using java 1.6.0_20.. So, the new version solved the issue.. – shadyabhi May 07 '10 at 04:04

2 Answers2

39

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);
    }
}
ablaeul
  • 2,750
  • 20
  • 22
13

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);
        }
    });
Bat0u89
  • 610
  • 2
  • 17
  • 25
  • 1
    `frame.addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e){ Dimension d= frame.getSize(); Dimension minD=frame.getMinimumSize(); if(d.width Where frame is declared as class member `private JFrame frame;` [Demo Code, need to download](https://ideone.com/i9AYBf) – Abhijeet Sep 22 '16 at 08:55