Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get maximized size of frame?

Tags:

java

swing

jframe

I set maximized frame: setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);

Now how I can get this size, because when I call getSize() or getPreferredSize it returns 0 0?

like image 793
hudi Avatar asked Nov 02 '11 22:11

hudi


1 Answers

You will get the maximized size correctly after the setVisible(true); is executed.

public NewJFrame() {                            // Constructor
    initComponents();
    this.setExtendedState( getExtendedState() | JFrame.MAXIMIZED_VERT | Frame.MAXIMIZED_HORIZ);
    // height and width still prints the original values 
    System.out.println(this.getSize().height + " " + this.getSize().width); 
}

....

public static void main(String args[]) {        // main
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            NewJFrame foo = new NewJFrame();
            foo.setVisible(true);
            // after setVisible(true) actual maximized values
            System.out.println(foo.getSize().height + " " + foo.getSize().width);
        }
    });
}
like image 53
Costis Aivalis Avatar answered Oct 04 '22 21:10

Costis Aivalis