Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 313
Paul Morrison Avatar asked Sep 30 '11 16:09

Paul Morrison


People also ask

How do I stop a JFrame from closing?

To disable the close operation on a JFrame , when user click the close icon on the JFrame , we can set the value of the default close operation to WindowConstants. DO_NOTHING_ON_CLOSE .

How do you hide a frame window in Java?

For hiding a JFrame, setVisible(false) is correct (and again besides the deprecated hide() again the only way). Depending on if you plan to eventually reuse the frame (show it again in future) you may additionally want to additionally call dispose() if you will not show the frame again.

Which is the method used for window closing event?

The windowClosing() method is found in WindowListener interface and WindowAdapter class. The WindowAdapter class implements WindowListener interfaces. It provides the default implementation of all the 7 methods of WindowListener interface.

How do you pause a swing in Java?

In Java, we can use TimeUnit. SECONDS. sleep() or Thread. sleep() to pause a program for a few seconds.


1 Answers

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.

like image 91
developmentalinsanity Avatar answered Sep 19 '22 17:09

developmentalinsanity