Extended jframe size?

How can I get the width and height of an extended JFrame? (done)

setExtendedState( getExtendedState() | JFrame.MAXIMIZED_BOTH); 
+4
source share
3 answers

After frame.setVisible(true) :

 double width = frame.getSize().getWidth(); double height = frame.getSize().getHeight(); 
+1
source

Adding to my comments:

uhm wrong with calling getWidth and getHeight after setting up JFrame for JFrame.MAXIMIZED_BOTH

Are you sure you call it after the JFrame is visible and the size state is resized? i.e:

 frame.setExtendedState( getExtendedState() | JFrame.MAXIMIZED_BOTH); frame.pack(); frame.setVisible(true); System.out.println(frame.getWidth()+" "+frame.getHeight()); 

Another solution is to simply get the size of the screens with

 GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); Rectangle bounds = env.getMaximumWindowBounds(); System.out.println("Screen Bounds: " + bounds ); 

since it will be the size of the JFrame in the JFrame.MAXIMIZED_BOTH state

+4
source
 frame.getContentPane().getSize(); 

You will lose the title bar size of the decoration. Or get rootPane, even better.

+1
source

All Articles