Java - how do I prevent WindowClosing from actually closing the window

后端 未结 7 1684
灰色年华
灰色年华 2020-12-03 04:23

I seem to have the reverse problem to most people. I have the following pretty standard code to see if the user wants to do some saves before closing the window:

         


        
7条回答
  •  悲&欢浪女
    2020-12-03 04:58

    To solve the same problem I tried the very first answer of this article. As separate application it works, but not in my case. Maybe difference is in JFrame(in answer) and FrameView (my case).

    public class MyApp extends SingleFrameApplication { // application class of my project
      ...
      protected static MyView mainForm; // main form of application
      ... 
    }  
    public class MyView extends FrameView {
        ...
        //Adding this listener solves the problem. 
        MyApp.getInstance().addExitListener(new ExitListener() {
    
          @Override
          public boolean canExit(EventObject event) {
            boolean res = false;
            int reply = JOptionPane.showConfirmDialog(null,
                    "Are You sure?", "", JOptionPane.YES_NO_OPTION);
            if (reply == JOptionPane.YES_OPTION) {
              res = true;
            }
            return res;
          }
          @Override
          public void willExit(EventObject event) {
          }
        });
        ...
    }   
    

提交回复
热议问题