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.
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).
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.
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).
windowClosing
is part of the WindowListener
interface. Use addWindowListener
instead of addWindowStateListener
.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With