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

JavaSwingJframeWindowlistener

Java Problem Overview


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:

  frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
  frame.addWindowListener(new WindowAdapter() {
     public void windowClosing(WindowEvent ev) {
      boolean close = true;
         // check some files, asking if the user wants to save
         // YES and NO handle OK, but if the user hits Cancel on any file,
         //   I want to abort the close process  	
         // So if any of them hit Cancel, I set "close" to false
      if (close) {
          frame.dispose();
	      System.exit(0);
         }
       }			
});

No matter what I try, the window always closes when I come out of windowClosing. Changing WindowAdapter to WindowListener doesn't make any difference. What is weird is that the documentation explicitly says "If the program does not explicitly hide or dispose the window while processing this event, the window close operation will be cancelled," but it doesn't work that way for me. Is there some other way of handling the x on the frame? TIA

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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

Solution 3 - Java

not sure where is your problem,

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ClosingFrame extends JFrame {

    private JMenuBar MenuBar = new JMenuBar();
    private JFrame frame = new JFrame();
    private static final long serialVersionUID = 1L;
    private JMenu File = new JMenu("File");
    private JMenuItem Exit = new JMenuItem("Exit");

    public ClosingFrame() {
        File.add(Exit);
        MenuBar.add(File);
        Exit.addActionListener(new ExitListener());
        WindowListener exitListener = new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                int confirm = JOptionPane.showOptionDialog(frame,
                        "Are You Sure to Close this Application?",
                        "Exit Confirmation", JOptionPane.YES_NO_OPTION,
                        JOptionPane.QUESTION_MESSAGE, null, null, null);
                if (confirm == JOptionPane.YES_OPTION) {
                    System.exit(1);
                }
            }
        };
        frame.addWindowListener(exitListener);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setJMenuBar(MenuBar);
        frame.setPreferredSize(new Dimension(400, 300));
        frame.setLocation(100, 100);
        frame.pack();
        frame.setVisible(true);
    }

    private class ExitListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            int confirm = JOptionPane.showOptionDialog(frame,
                    "Are You Sure to Close this Application?",
                    "Exit Confirmation", JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE, null, null, null);
            if (confirm == JOptionPane.YES_OPTION) {
                System.exit(1);
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                ClosingFrame cf = new ClosingFrame();
            }
        });
    }
}

Solution 4 - Java

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
}

Solution 5 - Java

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();
                            }
			}								
		});

Solution 6 - Java

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) {
      }
    });
	...
}	

Solution 7 - Java

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

view this link

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaul MorrisonView Question on Stackoverflow
Solution 1 - JavadevelopmentalinsanityView Answer on Stackoverflow
Solution 2 - JavaBenColeView Answer on Stackoverflow
Solution 3 - JavamKorbelView Answer on Stackoverflow
Solution 4 - JavaJibin MathewView Answer on Stackoverflow
Solution 5 - JavaTharindu MalawaraarachchiView Answer on Stackoverflow
Solution 6 - Javauser3463820View Answer on Stackoverflow
Solution 7 - Javauser6195279View Answer on Stackoverflow