File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Cancelling close in windowClosing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Cancelling close in windowClosing" Watch "Cancelling close in windowClosing" New topic

Cancelling close in windowClosing

Randy Anarcane

Joined: Apr 26, 2004
Posts: 2
It is supposedly possible to cancel the closing of a window within the windowClosing() method. This is supposedly done by simply not closing it within the method, or that's what I'm understanding from the Java documentation. However, I have not found this to be the case at all.

The JOptionPane pops up just fine after I click the X, but the window closes regardless of whether I choose yes or no afterward. How do I properly go about preventing the window from closing?
[ April 26, 2004: Message edited by: Randy Anarcane ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

Add the following line to your code before the addWindowListener...

The default close operation when this is not specified is to dispose of the JFrame. So it doesn't matter what you do it's going to close unless you specify otherwise.
The windowClosing event is more or less a cleanup method so that you can tie up any loose ends before the window closes.
Also note that dispose() only kills the JFrame. It does not do the same thing as System.exit(0). I am sure you know that, but I wanted to make sure.

GenRocket - Experts at Building Test Data
Randy Anarcane

Joined: Apr 26, 2004
Posts: 2
Vielen Dank.
I agree. Here's the link:
subject: Cancelling close in windowClosing
It's not a secret anymore!