简体   繁体   中英

Closing two JDialogs at the same time

Let's suppose two JDialogs, j1 and j2, and I need j2 to be closed when I hit the "X" button on the j1.

I tried to implement WindowListener on j1, and I used j2.dispose() in the windowClosing() and the windowClosed() methods but it didn't work.

Is it possible to put an actionPerformed on the "X" button for example? or is it possible to do it using a windowListener and how?

Thank you in advance.

My guess is you forgot to add your WindowListener . Put a System.out.println in your WindowListener methods to see if they're actually being called.

Here's a working example of what you describe:

public class Test {

    public static void main(String[] args) {
        final JDialog jd1 = new JDialog((JFrame) null, "Dialog 1", false);
        jd1.setVisible(true);

        JDialog jd2 = new JDialog((JFrame) null, "Dialog 2", false);
        jd2.setVisible(true);
        jd2.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent we) {
                jd1.dispose();
            }
        });
    }
}

Is it possible to put an actionPerformed on the "X" button for example?

Check out my answer in this posting: Associate predefined action to close button Java JDialog

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