Karan Goel
Karan Goel

Reputation: 1117

How to run a method only after a frame has been closed?

So, my code right now looks like this:

Class2 className = new Class2(param1, param2);
className = null;
if (Class2 == null) {
    refreshState();
}

I want the refreshState method to run once the className object is destroyed. So basically Class2 is a class that runs another frame on top of my existing frame. I want the method to run only when the new frame has been closed. How can I do this?

Upvotes: 4

Views: 276

Answers (2)

Anthony
Anthony

Reputation: 1333

I would say add a java.awt.event.WindowListener to the frame:

class2.getFrame().addWindowsListener(new WindowAdapter() {
    public void windowClosed(WindowEvent we) {
        // The frame is closed, let's dot something else
        refreshState();
    }
}

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

So basically Class2 is a class that runs another frame on top of my existing frame. I want the method to run only when the new frame has been closed. How can I do this?

The better solution is to use modal dialogs such as a JOptionPane or a modal JDialog. This will stop the processing of the Swing code in the main window until the dialog window has been dealt with and is no longer visible. Then your refreshState can run immediately after the dialog has been closed:

Pseudocode:

main window code
show modal dialog
refresh state here. This will be called only after the dialog has returned.

Upvotes: 3

Related Questions