Java: how to cancel application exit
In one of my programs, I want to display a dialog when the user tries to exit the application The user must then choose to save certain states of the program rather than save or cancel the exit operation
I wrote this to first find a solution and then implement it:
import javax.swing.*; import java.awt.Dimension; import java.awt.event.*; class WL implements WindowListener { private boolean statussaved; private JFrame tframe; WL (JFrame frame) { statussaved = false; tframe = frame; } @Override public void windowActivated (WindowEvent w) { } @Override public void windowClosed (WindowEvent w) { } @Override public void windowDeactivated (WindowEvent w) { } @Override public void windowDeiconified (WindowEvent w) { } @Override public void windowIconified (WindowEvent w) { } @Override public void windowOpened (WindowEvent w) { } @Override public void windowClosing (WindowEvent w) { if (statussaved) { return; } final JDialog diag = new JDialog (tframe,"Save Progress",true); diag.setPreferredSize (new Dimension (500,100)); diag.setResizable (false); diag.setDefaultCloSEOperation (JDialog.DISPOSE_ON_CLOSE); JPanel notifypanel = new JPanel (); notifypanel.add (new JLabel ("Do you want to save the current status ?")); JButton yesbutton = new JButton ("Yes"); JButton nobutton = new JButton ("No"); JButton cancelbutton = new JButton ("Cancel"); yesbutton.addActionListener (new ActionListener () { @Override public void actionPerformed (ActionEvent a) { //SAVE THE STATUS System.out.println ("Saving status..."); statussaved = true; diag.dispose (); tframe.dispose (); } }); nobutton.addActionListener (new ActionListener () { @Override public void actionPerformed (ActionEvent a) { //just exit/close the application diag.dispose (); tframe.dispose (); } }); cancelbutton.addActionListener (new ActionListener () { @Override public void actionPerformed (ActionEvent a) { //DON'T EXIT !!! diag.dispose (); } }); notifypanel.add (yesbutton); notifypanel.add (nobutton); notifypanel.add (cancelbutton); diag.setContentPane (notifypanel); diag.pack (); diag.setVisible (true); } } public class SaveTest { public static void main (String[] args) { SwingUtilities.invokelater (new Runnable () { @Override public void run () { JFrame frame = new JFrame ("Save Test"); frame.setDefaultCloSEOperation (JFrame.DISPOSE_ON_CLOSE); frame.addWindowListener (new WL (frame)); JLabel lab = new JLabel ("just some information"); frame.setPreferredSize (new Dimension (400,300)); frame.setResizable (false); frame.add (lab); frame.pack (); frame.setVisible (true); } }); } }
It compiles and runs without any change, so you can test it
The yes and no options work as expected, but I have no idea what to write in the actionlistener of the Cancel button What I want is that when the user clicks the "Cancel" button, the dialog box disappears, but the main window is still visible (that is, the program continues to run)
Now, since all this is implemented in the windowclosing method, it is obvious that some kind of processing signal is sent to destroy the JFrame This means that this may not be possible in the current design I don't mind reorganizing / redesigning all this to make it work It's just... I don't know how
Any ideas?
Solution
replace
mainframe.setDefaultCloSEOperation (JFrame.DISPOSE_ON_CLOSE);
with
mainframe.setDefaultCloSEOperation (JFrame.DO_NOTHING_ON_CLOSE);
If the user cancels the shutdown – do nothing If agreed – manually call dispose()