Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to stop program from closing 1

Status
Not open for further replies.

TGJ

Programmer
Jan 31, 2005
64
CA
Hi, my program is closing if the user presses "yes" or "no" when asked if they want to quit after pressing the "x" on the top right corner of the program. I am wondering how I can keep the program running if they press "no". Thanks.
Here is the code:
Code:
public void closeProgram()
{
    int answer = 0;
    answer = JOptionPane.showConfirmDialog(this, "Are you sure you want to quit?", "ExitProgram?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

    if (answer == JOptionPane.YES_OPTION)
    {
        dbAccess.close();
        db.CloseDB();
        System.exit(0);
    }
}
 
Seems OK to me.

How are you calling that method?

Cheers,
Dian
 
System.exit(0) is not called if the user chooses option "no".

This is how the method is called:

Code:
addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                closeProgram();
            }
        });
 
Well sounds like the whole app is shutting down any way (windowClosing event) - so is there something you are not showing us ?

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Humm

Maybe you need a
Code:
 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
for your Frame

Cheers,
Dian
 
Thanks now it works great here is how the code looks now.

Code:
addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
                closeProgram();
            }
        });
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top