I am making a contact book (for name & phone number). I am adding all contacts (as an object) in an ArrayList. I have made Save and SaveAs. I want to make "Exit" option in JMenu (in Swing menu) which when user clicks should exit the program but before exiting it should check whether file is modified, if modified it should prompt to save or discard. here is my code, but I dont think this is the right way
private void mnuExitActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
/*if(file is modifled)
{
JOptionPane.showConfirmDialog();
}
else
{
System.exit(0);
}*/
}
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
given the various number of ways to close an app e.g. alt-F4, coffee cup icon menu/close, 'X' etc, you should:
1) put your confirm close action in a windowListener's windowClosing
2) set the frame to DO_NOTHING_ON_CLOSE
3) menuItem's exit to create/process a windowClosing event
this way, however it is closed (turning off power excepted), you will be able to check if file modified etc
charles dexter
Greenhorn
Joined: May 21, 2009
Posts: 5
posted
0
Thanks for your reply, I am new to this. Would you be able to write some code? Thanks
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
> Would you be able to write some code?
do you want my PayPal details now?
charles dexter
Greenhorn
Joined: May 21, 2009
Posts: 5
posted
0
what are your charges?
pete stein
Bartender
Joined: Feb 23, 2007
Posts: 1561
posted
0
charles dexter wrote:what are your charges?
You know of course that he's kidding you. Why not try to use the suggestions and make an attempt at solving your problem? You can always come back with your attempt code if it doesn't work.
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
> what are your charges?
my paypal remark was a 'tongue-in-cheek' comment.
most here will gladly help/nudge you in the right direction,
but frown at requests to provide complete code.
you should have no problems implementing (1) or (2),
but for (3) you need to create a new WindowEvent(...)
if you google "new WindowEvent" (including quotes),
you should find examples of how to create it, and how
to process it.
if you run into problems, post your attempt here
charles dexter
Greenhorn
Joined: May 21, 2009
Posts: 5
posted
0
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.
//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);
}
}
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
> Now whatever option I select (Yes, No or Cancel), it saves the file.
that's the way you've coded it, assuming WIDTH does equal 1.
JOptionPane.showConfirmDialog(..) returns a static int
public static int showConfirmDialog(Component parentComponent,Object message,String title,int optionType)
so you need to check the return value to see if it is "YES".
you have it just displaying the message, and doing nothing with the return value.
and when you add the missing bit, do yourself a favor and use the variable names, not 0,1,2 etc as you've done here
JOptionPane.showConfirmDialog(this, "Save?", "Confirm", 1); which reads much better as
JOptionPane.showConfirmDialog(this, "Save?", "Confirm", JOptionPane.YES_NO_CANCEL_OPTION);