[solved] WindowAdapter, how to cancel window closing event?

Hey JGO, at the moment I’m trying to program a gui that has a button to shutdown the server, and also has another method of shutting down the server. (Relating to my GNetLib Networking Server)
(The JFrame’s “X” icon)

When I click the JButton “Shutdown The Server” I have the option of yes/no/cancel and everything works as expected.

However when I click the JFrame’s “X” icon in the top right it decides to ignore my yes/no clicks and exits anyways.

Is there a method like event.cancel()?

Here’s my code so you know what I’m trying to accomplish:
(Method is called when button to shutdown is clicked, or when JFrame’s exit “X” icon is clicked)

Code that handles the JFrames “X” icon
:


		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				confirmServerShutdown();
			}
		});


protected void confirmServerShutdown() {
		int option = JOptionPane.showConfirmDialog(frame,
						"Are you sure you want to shut down the server?\nNOTE: This will disconnect all online clients!",
						"Confirm server shutdown?", 
						JOptionPane.YES_NO_OPTION,
						JOptionPane.QUESTION_MESSAGE);
		if (option == 0) { // Yes option selected
			System.out.println("Executing GUI/GNetServer shutdown...");
			server.shutDownServer();
			Runtime.getRuntime().exit(0);
		} else if (option == 1) { // No option selected
			// How to prevent shutdown when I clicked no!?
			return;
		}
	}

Here are some pictures so you can hopefully get a better understanding of what the problem is:

Picture 1:

Picture 2:

Basically what I’m asking is how can I (With my WindowListener -> listening for window closing events etc) cancel the event?

Thanks for any help JGO, looking forward to your replys :slight_smile:
(I haven’t messed with Swing/GUI’s in a while :P)

Updated the OP with more mentionable code that’s relative to my issue. ::slight_smile:

Looking at this post: http://stackoverflow.com/a/7613653

CopyableCougar4

Ahhhh thank you, “WindowConstants.DO_NOTHING_ON_CLOSE” than I’ll be able to handle the event myself, thanks mate.

If it works I’ll post back :slight_smile:

Both closing methods work as expected after your help, thanks CopyableCougar4!

Appreciated++