Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

close window event in java

I added an window state listener as follow:

this.addWindowStateListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            ExitAction.getInstance().actionPerformed(null);
        }

    });

But when I'm using the X close button the event is not called. I think it's something to do with netbean jdesktop framework. But I can't find what could be the problem. Thanks for your help.

like image 533
Guy Avatar asked Dec 31 '09 06:12

Guy


People also ask

What is addWindowListener in Java?

public interface WindowListener extends EventListener. The listener interface for receiving window events. The class that is interested in processing a window event either implements this interface (and all the methods it contains) or extends the abstract WindowAdapter class (overriding only the methods of interest).

What is the use of WindowListener?

Window listeners are commonly used to implement custom window-closing behavior. For example, a window listener is used to save data before closing the window, or to exit the program when the last window closes.

How do you close a frame in a swing?

You can easily close your JFrame by clicking on the X(cross) in the upper right corner of the JFrame. However JFrame. setDefaultCloseOperation(int) is a method provided by JFrame class, you can set the operation that will happen when the user clicks the X(cross).


3 Answers

windowClosing is part of the WindowListener interface. Use addWindowListener instead of addWindowStateListener.

like image 132
jackrabbit Avatar answered Sep 19 '22 07:09

jackrabbit


Normally you use a WindowListener for this.

Check out Closing an Application for an approach I use, although I must admit I've never tried it with Netbeans since I don't use an IDE.

like image 28
camickr Avatar answered Sep 21 '22 07:09

camickr


Not answering your question directly (since an answer has already been given), but I assume you want to quit your program (or just hide a window) on exit. There is a shorter solution for these situations:

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
like image 43
Bozho Avatar answered Sep 20 '22 07:09

Bozho