Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

making "Exit" option in JMenu

 
charles dexter
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply, I am new to this. Would you be able to write some code? Thanks
 
Michael Dunn
Ranch Hand
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Would you be able to write some code?

do you want my PayPal details now?
 
charles dexter
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what are your charges?
 
pete stein
Bartender
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Bartender
Pie
Posts: 1227
10
Eclipse IDE Google Web Toolkit Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Maneesh Godbole
Saloon Keeper
Posts: 10971
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles, in future, please use code tags. You can edit your existing posts to incorporate the code tags by clicking on the button
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic