Java: how to cancel application exit

In one of my programs, I want a dialog box to appear when a user tries to exit the application. Then the user must choose to save some state of the program, rather than saving or canceling the exit operation.

I wrote this, trying to find a solution first 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 changes, so you can test it.

The Yes and No options work as expected, but I have absolutely no idea what to write the ActionListener buttons in the ActionListener . I want, when the user clicks the "Cancel" button, the dialog box disappears, but the main window remains visible (i.e. the program continues to work).

Now, since all this is implemented in the windowClosing method, it is clear that some dispose JFrame was sent to destroy the JFrame. This means that probably this cannot be done in the current project. I am not against reorganizing / redesigning all of this to make it work. It's just ... I don't know how.

Any ideas?

+6
source share
2 answers

Replace

 mainframe.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE); 

from

 mainframe.setDefaultCloseOperation (JFrame.DO_NOTHING_ON_CLOSE); 

If the user cancels the close, do nothing. If you agree, call dispose() manually.

+9
source

Check out the JOptionPane that handles most of this stuff for you, for example

 JOptionPane.showConfirmDialog(frame, "please choose one", "information", OptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE); 

Your DefaultCloseOperation must be DO_NOTHING_ON_CLOSE so that your dialog can handle things - otherwise the window will be deleted before the user can cancel it. Then you manually close the window or exit the application or something according to the user's choice.

+6
source

Source: https://habr.com/ru/post/908259/


All Articles