Luke
Luke

Reputation: 3872

Is there a Java WindowListener that gets called just before a Window is shown?

Is there a Java WindowListener that gets called just before a Window is shown?

I've tried both windowOpened (example below) and componentShown. Both of these get called just after the window is shown. Is there any listener that gets called before the window is shown?

window.addWindowListener(new WindowAdapter() {
    @Override
    public void windowOpened(WindowEvent evt) {
        < ... code ... >    
    }
});

Upvotes: 1

Views: 156

Answers (1)

Zhedar
Zhedar

Reputation: 3510

As you wish here's an answer compiled by the comments.(Maybe sometimes somebody will search for that ;) )
The solution here may to override your JFrame's setVisible(boolean)-method by subclassing JFrame.

That method could look like this:

@Override
public void setVisible(boolean visible)
{
   if(visible) //Window is going to be shown
   {
       //your code you want to be executed before window is shown
   }

   //finally show or hide window
   super.setVisible(visible);
}

As in this case you need to refactor your code to use the new subclass, just find and replace every "new JFrame" to "new YourFrameSub"(every editor or IDE should be able to do that).
This way you can ensure your code is executed, before anything is shown.
Remember to use a SwingWorker or sth. like that to show something like a ProgressBar in a JDialog to show the user that something will take longer than expected. Of course this only takes place if you're planning to execute a long running action like doing IO-operations.

Upvotes: 1

Related Questions