Cancel window closing operation in java app

Question: In my Java swing crossplatform project I have to ask the user if he/she really wants to quit from the desktop application before exit, so I have to handle the WindowClosing event. My problem is simple. I expected the DO_NOTHING_ON_CLOSE flag to prevent the form from closing. I didn’t thought the WindowClosing event will be fired even if the setDefaultCloseOperation was set to DO_NOTHING. When I even click on the NO button the window does close! Have you experienced it before and what have you done? Thank you in advance. I am attaching my code. Answer: The answer is in the org.jdesktop.application.Application.ExitListener interface documentation. Read more from here. This web page contains a SingleFrameApplication example code that shows how you might implement an ExitListener object. The exit method calls the shutdown method only if all listeners approve the request to exit. Your code is:
    // In my .app class:
    public class MyProjectApp extends SingleFrameApplication {
   
        @Override protected void startup() {
            show(new MyProjectAppView(this));
        }
        // ....
        public static void main(String[] args) {
            launch(MyProjectApp.class, args);
        }
    }
   

    // ...... In MyProjectAppView constructor:
        JFrame frm = getFrame();

        // I set the DO_NOTHING_ON_CLOSE flag here:
        frm.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frm.setResizable(false);

        // I add closing listener
        frm.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                CloseApp();
            }
        });
         / /....

        @Action
        public void CloseApp(){
            // Returns YES = true , NO = false
            if( Question("Do you really want to quit?") ){
                getFrame().dispose();
                System.exit(0);
            }
        }
The Application class implements an exit method to shut down your application. According to the Java documentation, this process involves asking any ExitListener objects whether exiting is possible, then alerting those same listeners that the Application will actually shut down. Regardless of how you finally call the exit method, you should override it to perform application-specific cleanup. It should work for you:
     // Insert into View constructor
        this.getApplication().addExitListener(new ExitHandler());
     // ...
     //
     // Add inner class event handler for app exit to your View class
    class ExitHandler implements org.jdesktop.application.Application.ExitListener
    {
        public boolean canExit(EventObject e) {
            // In your CloseApp() method set boolean flag bCanExit to true or false

            // By the way it is a perfect place for App cleanup

            return bCanExit;  // true or false
        }
        public void willExit(EventObject e)
        {
        }
    }