JDialog with WindowListener - windowClosing not fired
Asked Answered
P

1

5

I have a class that extends JDialog that have a window listener:

class MyClass extends JDialog {

     public MyClass() {
         setDefaultCloseOperation( JDialog.DO_NOTHING_ON_CLOSE );
         addWindowListener( new WindowAdapter() {

                @Override
                public void windowClosing(WindowEvent e) {
                    System.out.println("closing...");
                    //do something...
                }

            });
     }

}

When i click in the X button, nothing happens. And I don't see the print of "closing..."

What I am missing?

Precisian answered 30/12, 2011 at 16:13 Comment(1)
Code looks reasonable, post an SSCCE that demonstrates the problem.Homoeroticism
G
10

Works for me. See also this related example.

import java.awt.EventQueue;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JDialog;

class MyClass extends JDialog {

    public MyClass() {
        setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("closing...");
                //do something...
            }
        });
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                MyClass m = new MyClass();
                m.setVisible(true);
            }
        });
    }
}
Garbage answered 30/12, 2011 at 16:28 Comment(2)
Thanks for example. I figured out that i'm was calling setVisible before adding the window listener...Precisian
Behold, the power of an sscce! I'm pretty sure I've done something similar, too. :-)Garbage

© 2022 - 2024 — McMap. All rights reserved.