2
JPanel scorePanel = new JPanel();
scorePanel.setBackground( new Color( 95 , 8 , 248 , 255 ));
JLabel scoreLabel = new JLabel( "Score : 0"  );
scoreLabel.setForeground( Color.white );
scoreLabel.setFont( new Font( "Cambria" , Font.BOLD , 20 ) );
scorePanel.add(scoreLabel);
Dimension d =new Dimension( scorePanel.getSize() );
System.out.println(d);

The output of this snippet when run along with the whole code is-java.awt.Dimension[width=0,height=0]

Why do i get this output, when in scorepanel i have a label of size 20 ? Is this the actual size of scorepanel ?

Heisenbug
  • 38,762
  • 28
  • 132
  • 190
Suhail Gupta
  • 22,386
  • 64
  • 200
  • 328

1 Answers1

3

No, it's not. Once it's been realized (i.e. packed into a container and rendered), the actual dimensions of the component will be made available.

mre
  • 43,520
  • 33
  • 120
  • 170
  • what does `0,0` indicate ? I get this output only after packing into a container – Suhail Gupta Jun 24 '11 at 15:31
  • 2
    The layout manager doesn't set the size to (0, 0), The default size and location of all components are (0, 0), when the component is created. The layout manager will determine the appropriate size and location whenever it is invoked which is basically whenever the frame is make visible or resized or a component is validated. – camickr Jun 24 '11 at 15:39
  • yes i get the result **java.awt.Dimension[width=400,height=35]** – Suhail Gupta Jun 24 '11 at 15:41
  • @camickr, thanks for the clarification. I removed my misleading comment. :) – mre Jun 24 '11 at 15:41
  • @ little bunny foo foo **what is rendering ?** – Suhail Gupta Jun 24 '11 at 16:05
  • @Subhail Gupta, Once it's "visible". – mre Jun 24 '11 at 16:06