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

后端 未结 7 1647
灰色年华
灰色年华 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:39

    This is the key, methinks: frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); Makes the difference in the test case I cooked up.

    0 讨论(0)
  • 2020-12-03 04:39

    For the handling of this thing do:
    if the user selects yes then use setDefaultCloseOperation(DISPOSE_ON_CLOSE); within the curly braces of that if else

    if a cancel is selected then use setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

    Consider example:

    int safe = JOptionPane.showConfirmDialog(null, "titleDetails!",  "title!!", JOptionPane.YES_NO_CANCEL_OPTION);
    
    if(safe == JOptionPane.YES_OPTION){
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);//yes
    
    } else if (safe == JOptionPane.CANCEL_OPTION) {
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);//cancel
    } else {
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);//no
    }
    
    0 讨论(0)
  • 2020-12-03 04:47

    Not sure where your problem is, but this works for me!

    frame.addWindowListener(new WindowAdapter() {
    
                public void windowClosing(WindowEvent evt) {
                                int res=JOptionPane.showConfirmDialog(null,
                                        "Do you want to exit.?");
                                if(res==JOptionPane.YES_OPTION){
                                        Cal.this.dispose();
                                }
                }                               
            });
    
    0 讨论(0)
  • 2020-12-03 04:57

    I've just tried this minimal test case:

    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    import javax.swing.JFrame;
    import javax.swing.WindowConstants;
    
    public class test {
    
        public static void main(String[] args) {
            final JFrame frame = new JFrame("Test");
            frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
            frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent ev) {
                    //frame.dispose();
                }
            });
            frame.setVisible(true);
    
        }
    
    }
    

    If I keep the dispose call commented, and hit the close button, the window doesn't exit. Uncomment that and hit the close button, window closes.

    I'd have to guess that something is wrong in your logic to set your "close" variable. Try double checking that.

    0 讨论(0)
  • 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) {
          }
        });
        ...
    }   
    
    0 讨论(0)
  • 2020-12-03 05:02

    setDefaultCloseOperation() method helps in the problem .https://chortle.ccsu.edu/java5/Notes/chap56/ch56_9.html

    view this link

    0 讨论(0)
提交回复
热议问题