Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>When I quit the application, optionDialog will pop out and it will ask me if I want to save the file before quit.</p> </blockquote> <ul> <li><p>pseudo code, <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html" rel="nofollow">Application is <code>JFrame</code></a> with <code>setDefaultCloseOperation(EXIT_ON_CLOSE);</code></p></li> <li><p>added <a href="http://docs.oracle.com/javase/tutorial/uiswing/events/windowlistener.html" rel="nofollow"><code>WindowListener</code> to <code>JFrame</code></a></p></li> <li><p>override <code>public void windowClosing()</code>, then every events <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html#button" rel="nofollow">firing <code>JOptionPane</code> with required choices</a></p></li> </ul> <p>for example</p> <pre><code>import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ClosingFrame extends JFrame { private JMenuBar MenuBar = new JMenuBar(); private JFrame frame = new JFrame(); private static final long serialVersionUID = 1L; private JMenu File = new JMenu("File"); private JMenuItem Exit = new JMenuItem("Exit"); public ClosingFrame() { File.add(Exit); MenuBar.add(File); Exit.addActionListener(new ExitListener()); WindowListener exitListener = new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { int confirm = JOptionPane.showOptionDialog(frame, "Are You Sure to Close this Application?", "Exit Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null); if (confirm == JOptionPane.YES_OPTION) { System.exit(1); } } }; frame.addWindowListener(exitListener); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setJMenuBar(MenuBar); frame.setPreferredSize(new Dimension(400, 300)); frame.setLocation(100, 100); frame.pack(); frame.setVisible(true); } private class ExitListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { int confirm = JOptionPane.showOptionDialog(frame, "Are You Sure to Close this Application?", "Exit Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null); if (confirm == JOptionPane.YES_OPTION) { System.exit(1); } } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { ClosingFrame cf = new ClosingFrame(); } }); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload