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

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

    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
    }
    

提交回复
热议问题