tags:

views:

261

answers:

4

Hi,

I have a component (JPanel) inside a Window. I always get false when calling panel.isShowing(), when calling from a windowGainedFocus() event (when the parent window gets focus).

I assume that when the windowGainedFocus() event is called, the painting of the JPanel within this Window had not been finished yet.

I was trying to place that call isShowing() on the paint() method of this Window, but I always get isShowing() = false.

Is there a way I could get an event when the JPanel is fully shown on screen and the isShowing() method will return true ?

Thanks

+5  A: 

You should probably best approach this with a hierarchy listener on the panel itself:

panel.addHierarchyListener(new HierarchyListener() {
    public void hierarchyChanged(HierarchyEvent e) {
        if ((HierarchyEvent.SHOWING_CHANGED & e.getChangeFlags()) !=0 
             && panel.isShowing()) {
          //do stuff
        }
    }
});
Yishai
A: 

If you don't want an event but have some specific code that needs to be run after your component has been drawn, you can override addNotify(), which gets called to make the component displayable. Example:

public void addNotify()
{
    super.addNotify();
    // at this point component has been displayed
    // do stuff
}
Steve Kuo
A: 

You component will be fully displayed after you receive WindowListener.windowActivated. You will also run into timing problems and race conditions trying to assign focus before the windowActivated event.

Software Monkey
A: 

It is good to use Listener in this condition. An Event occurs when the JPanel becomes visible and you catch the event and do your work. Yishai's solution is suitable.

Firstthumb