How do I make exit button to show confirm dialog box to save the modifed file

I am trying to make an exit button which prompts to save the modified file (if file is modified) before quitting. Now whatever option I select (Yes, No or Cancel), it saves the file.
Here is my code for closing.
private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        //int Cancel = 0;
     if(ExitIndex ==1)
     {
 //JOptionPane.showConfirmDialog(rootPane, mnuSave, path, WIDTH, mnIndex) ;
JOptionPane.showConfirmDialog(this, "Save?", "Confirm", 1);
if(WIDTH == 1)
{
            try
        {
            ObjectOutputStream outstream = new ObjectOutputStream(new FileOutputStream(path));
            outstream.writeObject(myList);
 
            //dataDisplay.append("Array written to disk file;" +path + '\n');
            outstream.close();
            JOptionPane.showMessageDialog(null, "Your file is saved successfully");
        }
        catch(IOException err1)
        {
            JOptionPane.showMessageDialog(null, "Sorry, " + err1.getMessage());
        }
 
}
 
  else
     {
            return;
     }
 
}
 
     else
     {
         System.exit(0);
     }
    }

Open in new window

chandanchoubeyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KatanagashiCommented:
Hi showConfirmationDialog returns a Integer that you most use in a if expression

here's some documentation on that:

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JOptionPane.html
0
CEHJCommented:
As Katanagashi says, you can do
    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        //int Cancel = 0;
        if(ExitIndex ==1)
        {
            //JOptionPane.showConfirmDialog(rootPane, mnuSave, path, WIDTH, mnIndex) ;
            int userChoice = JOptionPane.showConfirmDialog(this, "Save?", "Confirm", 1); 
            if(userChoice == JOptionPane.YES_OPTION && WIDTH == 1)
            {
                try
                {
                    ObjectOutputStream outstream = new ObjectOutputStream(new FileOutputStream(path));
                    outstream.writeObject(myList);
 
                    //dataDisplay.append("Array written to disk file;" +path + '\n');
                    outstream.close();
                    JOptionPane.showMessageDialog(null, "Your file is saved successfully");
                }
                catch(IOException err1)
                {
                    JOptionPane.showMessageDialog(null, "Sorry, " + err1.getMessage());
                }
 
            }
 
            else
            {
                return;
            }
 
        }
 
        else
        {
            System.exit(0);
        }
    }   

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chandanchoubeyAuthor Commented:
thanks! it worked
0
CEHJCommented:
:-) but probably should have been a points splits
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.