-2

When I try to get the bounds of a JPanel it always returns ( I use this.getBounds() in the constructor of the JPanel class):

java.awt.Rectangle[x=0,y=0,width=0,height=0]

At what point in the rendering of a Swing component does this actually get set? Is there an component event that happens after sizing of the bounds is made?

cj5
  • 785
  • 3
  • 12
  • 34

2 Answers2

1

In the constructor of a GUI, the components have not yet been rendered, and so their bounds will be [0, 0]. The components only get rendered after calling pack() or setVisible(true) on the top level window such as a containing JFrame. But your question begs the question of why do you need this information here? What will you do plan to do with this data? There are other locations where the information can be obtained and can be useful, such as a JComponent's paintComponent method or in a ComponentListener.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 3
    +1. Not only they haven't been rendered yet, but they probably haven't even been added to their container. So getting their bounds doesn't make sense at this point. – JB Nizet Aug 24 '14 at 21:42
  • It was more of a problem I am having with some other class, as I need to pass the parent's components rectangle object onto it. It's more than I really need to get into at this point, and is not relevant to my question. – cj5 Aug 24 '14 at 21:51
-2

Okay, thanks for the response, but I figured it out. Basically I need to override the paint() method from JComponent. At this point the rectangle bounds is properly produced. It really wasn't that complicated at all.

UPDATE:

Based on http://www.oracle.com/technetwork/java/painting-140037.html, it recommends using paintComponent() over paint() even though the API does allow it. This deals specifically with the component itself rather than all the related elements like borders and such.

cj5
  • 785
  • 3
  • 12
  • 34
  • No, please read the graphics tutorials in depth. There you'll see that you almost never want to override paint, for several reasons, but mainly because it is involved with more than just painting a component but also a component's borders and children. If not done right, you could mess this up. Better to override paintComponent as I mention in my answer, and to call the super method in the override. Link coming to you in a minute. – Hovercraft Full Of Eels Aug 24 '14 at 21:53
  • Links: 1) [Basic Swing Graphics Tutorial](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html). 2) [More advanced tutorial](http://www.oracle.com/technetwork/java/painting-140037.html). – Hovercraft Full Of Eels Aug 24 '14 at 21:56
  • There are other issues with doing this work in paint or even paintComponent, as you don't have full control over if or when the methods are called, and you do not want to have any code within these methods or called from these methods that takes any significant time to run since doing so can significantly decrease the perceived responsiveness of your GUI program. That's one reason why it might be better to use a ComponentListener, but again, it will depend on what you're trying to do with this. – Hovercraft Full Of Eels Aug 24 '14 at 21:58