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
(I haven’t messed with Swing/GUI’s in a while :P)