4

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

setExtendedState( getExtendedState() | JFrame.MAXIMIZED_BOTH);
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
andreihondrari
  • 5,743
  • 5
  • 30
  • 59

3 Answers3

4

Adding to my comment:

uhm whats wrong with calling getWidth and getHeight after setting JFrame to JFrame.MAXIMIZED_BOTH

are you sure you call it after JFrame is visible and size state has been changed? i.e:

frame.setExtendedState( getExtendedState() | JFrame.MAXIMIZED_BOTH);
frame.pack();
frame.setVisible(true);

System.out.println(frame.getWidth()+" "+frame.getHeight());

Another solution is simply get the screens size via

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

as that would be the size of JFrame in JFrame.MAXIMIZED_BOTH state

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • +1, almost... what if you have 2 monitors or more? :) -last time didn't worked for me: my frame was only in 1 monitor –  Dec 27 '12 at 12:36
  • @matheszabi good question perhaps going through all connected monitors and accumulating their widths/heights would do the trick – David Kroukamp Dec 27 '12 at 12:39
  • Your solution does work but there are some catches to it, as matheszabi said, and it does not depend on jframe aso. Eng. Fouad gave the right answer – andreihondrari Dec 27 '12 at 12:39
  • @AndrewG.H. **Eng. Fouad gave the right answer* yes he did but also I said to call `getWidth()`/`getHeight()` on `JFrame` after setting it extended forgot to say visible, which I added in my answer. It would be better to post an [SSCCE](http://sscce.org) next time, as than I and others could have seen you were not calling it before `JFrame` is visible/`pack()` is called – David Kroukamp Dec 27 '12 at 12:41
1
frame.getContentPane().getSize();

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

xpda
  • 15,585
  • 8
  • 51
  • 82
1

After frame.setVisible(true):

double width = frame.getSize().getWidth();
double height = frame.getSize().getHeight();
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417