1. /* User clicked on the X close-window button
  2.   * This method is the actionListener for the "windowClosing" event for the
  3.   * JFrame.
  4.   * To Create the ActionListener for the JFrame:
  5.   * Design view > Inspector panel
  6.   * Right-click on the JFrame, select Properties
  7.   * 1. Set "defaultCloseOperation" to DO_NOTHING
  8.   * 2. Click on Events. Scroll down to the "windowClosing" event.
  9.   * Click on the down-arrow button to the right of <none>.
  10.   * Press the Close button.
  11.   * The actionListener method will be inserted into your code.
  12.   */
  13. private void formWindowClosing(java.awt.event.WindowEvent evt) {
  14. shutdownApplication();
  15. }
  16.  
  17. /* Called by the actionListeners for File > Exit, and the
  18.   * red X close-window button.
  19.   */
  20. private void shutdownApplication() {
  21.  
  22. // Ask user if they really want to exit
  23. int choice = JOptionPane.showConfirmDialog(this,
  24. "Are you sure you want to exit?", "Confirm Exit",
  25. JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
  26. // Check user response
  27. if (choice == JOptionPane.OK_OPTION) {
  28. System.exit(0);
  29. }
  30. }