简体   繁体   中英

How to capture a JDialogs window closing event in a JFrame

I have a major JFrame running my application. It calls a JDialog class to perform a utility function, after performing its function, the user closes the dialog. I want the JFrame to detect that the JDialog has been closed. In other words how do I fire an event from the dialog when it's closed, and then catch it on the JFrame. Thank you.

You can implement a WindowListener and add it to the JDialog. The logic you seek after the JDialog is closed can be placed in the windowClosed or windowClosing method of the WindowListener. See the Window Listener Tutorial at Oracle for more information on how to implement this listener. Below is an example using the WindowAdapter class (empty implementation of a WindowListener) with the windowClosed method overridden:

myJDialog.addWindowListener(new WindowAdapter(){
    @Override
    public void windowClosed(WindowEvent e){
        myJframe.doSomething();
    }
});

you should add a Window Listener to your JDialog

jdialog.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
    System.out.println("jdialog window closed event received");
}

public void windowClosing(WindowEvent e) {
    System.out.println("jdialog window closing event received");
}
});

If you want the event be handled directly by a specific object, this object should implement the WindowAdapter. for example your jFrame.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM