tags:

views:

366

answers:

1

Which sort of listener do I have to add to a JFrame to detect when it is being hidden/shown via setVisible?

I tried using a WindowListener and the windowOpened and windowClosed methods, but they only work for the first time that a window is opened (windowOpened) or, respectively, when the window is closed using the dispose method (windowClosed). That is not enough for me. I want to be notified every time the window is made visible and invisible on the screen using setVisible.

Is there a standard swing way to achieve this, or do I need to make my own (by, say, overriding the setVisible method)?

+2  A: 

Try a java.awt.event.ComponentListener. You can add one using this code (where window is the name of the JFrame) :

window.addComponentListener(new ComponentAdapter() {
public void componentHidden(ComponentEvent e) 
{
    /* code run when component hidden*/
}
public void componentShown(ComponentEvent e) {
    /* code run when component shown */
}
});
James