How do I add functionality to the exit button without System.exit(0)?

JDN picture JDN · Aug 21, 2011 · Viewed 14.8k times · Source

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?

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);
        }
    }
}

Answer

βξhrαng picture βξhrαng · Aug 21, 2011

Which window's red button are you talking about?

This one?

enter image description here

If so, then you can implement a WindowListener for your JFrame:

JFrame f = ...;
f.addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent we) {
      System.exit(0);
   }
});

Or you can simply set the default close operation:

JFrame f = ...;
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);