title: How do I add functionality to the exit button without System.exit(0)?
body: <p>I went through and had the program System.exit(0) whenever the user clicked the window's red exit button. Is there a more efficient way to add functionality to that button?</p>
  
  <pre><code>import javax.swing.JOptionPane;
  import java.util.Scanner;
  
  class codeWithProblem
  {
      public static void main (String[] args)
      {
          String name, gender, passions, enjoy;
          int yesNo, permission, endProgram;
          String prefix = "";
          while (true)
          {
              name = JOptionPane.showInputDialog("Please enter your name: ");
              if (name.equals("null"))
              {
                  System.exit(0);
              }
              yesNo = JOptionPane.showConfirmDialog(null, "So your name is           "+name+". Is this correct?", "", JOptionPane.YES_NO_OPTION);
              if (yesNo == -1)
              {
                  System.exit(0);
              }
              if (yesNo == 0)
              {
                  break;
              }
          }
          while (true)
          {
              gender = JOptionPane.showInputDialog("Are you a boy or a girl?: ");
              if (gender.equals("null"))
              {
                  System.exit(0);
              }
              if ("boy".equalsIgnoreCase(gender)||"girl".equalsIgnoreCase(gender))
              {
                  break;
              }
          }
          if ("boy".equalsIgnoreCase(gender))
          {
              prefix = "Mr. ";
          }
          if ("girl".equalsIgnoreCase(gender))
          {
              prefix = "Ms. ";
          }
          JOptionPane.showMessageDialog(null, "It's nice to meet you "+prefix+name+".", "Hello", JOptionPane.PLAIN_MESSAGE);
          permission = JOptionPane.showConfirmDialog(null, "Lets ask some easy questions to start out with. Is this okay with you?: ",
          "", JOptionPane.YES_NO_OPTION);
          if (permission == 1)
          {
              endProgram = JOptionPane.showConfirmDialog(null, "Do you want to end this program? Click CANCEL to end and OKAY to continue.",
              "End Program?", JOptionPane.OK_CANCEL_OPTION);
              if (endProgram == 0)
              {
                  JOptionPane.showMessageDialog(null, "Good, lets continue...", "Continue", JOptionPane.PLAIN_MESSAGE);
                  permission = 0;
              }
              if (endProgram == 1)
              {
                  JOptionPane.showMessageDialog(null, "Goodbye....", "END", JOptionPane.PLAIN_MESSAGE);
              }
          }
          if (permission == 0)
          {
              passions = JOptionPane.showInputDialog("What are some of your passions?: ");
              if (passions.equals("null"))
              {
                  System.exit(0);
              }
              enjoy = JOptionPane.showInputDialog("Why do you enjoy "+passions+"?: ");
              if (enjoy.equals("null"))
              {
                  System.exit(0);
              }
              JOptionPane.showMessageDialog(null, "That's interesting", "hmmm", JOptionPane.PLAIN_MESSAGE);
              JOptionPane.showMessageDialog(null, "This program has no point and is going to end now.", "BACON", JOptionPane.PLAIN_MESSAGE);
          }
      }
  }
  </code></pre>