wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes making Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "making "Exit" option in JMenu" Watch "making "Exit" option in JMenu" New topic
Author

making "Exit" option in JMenu

charles dexter
Greenhorn

Joined: May 21, 2009
Posts: 5
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
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
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
> Would you be able to write some code?

do you want my PayPal details now?
charles dexter
Greenhorn

Joined: May 21, 2009
Posts: 5
what are your charges?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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
> 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
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:
if(ExitIndex ==1)
{
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);
}
}
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> 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);

so you'll end up with something like this

salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Here is my approach on solving issues:

Pseudo code:




Advantages:
You know there is a single method to perform all operations

Disadvantages:
There is a single method to perform all operations

My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268
    
    8

Charles, in future, please use code tags. You can edit your existing posts to incorporate the code tags by clicking on the button


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: making "Exit" option in JMenu