14
setExtendedState(getExtendedState()|JFrame.MAXIMIZED_BOTH);
setResizable(false);
setUndecorated(true);
System.out.println("--------> "+getContentPane().getWidth()); //----> 0 why is this happening?

I'm trying to determine the size of a JFrame. I've searched on google and checked the documentation and I still don't know why it won't work It works fine on about any other control I try it on

EDIT: frame.getWidth() works when called outside of the class (that extends JFrame) still, if I replace

System.out.println("--------> "+getContentPane().getWidth());

with

System.out.println("--------> "+this.getWidth());

getWidth will still return 0

EDIT2: I need the frame's size before setting it visible and stuff. I need to add other controls to the frame and their coordinates and size depend on the frame's size.

adrianton3
  • 2,258
  • 3
  • 20
  • 33
  • everything depends how you layed the container, if is there definitions for `setSize()` or `setBounds()`, then any reall `Dimension` returns only visible `Container`, otherwise if `JComponents` returns its `PreferedSize` to the `Container`, then you can get this `Dimension` anywhere you want – mKorbel Aug 09 '11 at 17:08
  • @madflame991, To your second edit, this is not possible, nor does it make sense. – mre Aug 09 '11 at 17:44

2 Answers2

13

The reason why you got 0 is because you didn't call any of the pack(), setSize(int, int) or setSize(Dimension). This is only when calling one of these method that the layout of your frame will be computed.

JFrame frame = new JFrame("My Frame");
frame.setExtendedState(getExtendedState()|JFrame.MAXIMIZED_BOTH);
frame.setResizable(false);
frame.setUndecorated(true);
frame.pack(); // Important line!!! 
frame.setVisible(true);
System.out.println("--------> "+getContentPane().getWidth());
aymeric
  • 3,877
  • 2
  • 28
  • 42
  • I need the width before I'm finished adding controls – adrianton3 Aug 09 '11 at 17:02
  • 2
    I run the test and it seems that you cannot get the frame width is it's not visible but I realized that if your frame is always JFrame.MAXIMIZED_BOTH, then the frame width is equal to the screen width, which you can get by `Toolkit tk = Toolkit.getDefaultToolkit();int screenWidth = tk.getScreenSize().width;` – aymeric Aug 09 '11 at 17:16
  • Thank you! getScreenSize is not what I was looking for but it pretty much solves my problem – adrianton3 Aug 09 '11 at 18:10
10

You haven't realized (i.e. setVisible(true)) the JFrame yet. And therefore, it has no size since it hasn't laid out its components.

mre
  • 43,520
  • 33
  • 120
  • 170
  • 2
    1+. `pack()` will also render the JFrame (I believe). – Hovercraft Full Of Eels Aug 09 '11 at 17:00
  • I tried calling it after making it visible and it still returned 0 – adrianton3 Aug 09 '11 at 17:01
  • @madflame991, This might be a stupid question, but did you add any components to the `JFrame` prior to making it visible? And as @Hovercraft mentioned, perhaps invoking `pack()` is the better option, since this will force the container to layout its components and then make itself visible. – mre Aug 09 '11 at 17:05
  • inc. @Hovercraft Full Of Eels suggestion +1 – mKorbel Aug 09 '11 at 18:31
  • 1
    *"`pack()` is the better option,"* Agree. *"..since this will force the container to layout its components"* Agree "*and then make itself visible."* No it doesn't. `pack()` is usually called just before `setVisible(true)`, but does not itself affect the visibility. – Andrew Thompson Aug 10 '11 at 02:26
  • @Andrew, You're completely right. I forgot to remove my comment! :D – mre Aug 10 '11 at 02:58