|
-
Sep 24th, 2005, 08:40 AM
#1
Thread Starter
Member
Prevent Application Quit
Hi, this is my application closing code. I have a confirmation dialog box to ask if the user wishes to close the application, what should I put if user decides not to quit the application?
Code:
public void windowClosing(WindowEvent we) {
int answer ;
answer = JOptionPane.showConfirmDialog(null,"Are you sure you want to exit?","Exit System",JOptionPane.YES_NO_OPTION) ;
if (answer == 0) {
System.out.println("You have successfully quit Restaurant System ver. 1.0 Built By: Lex Seetoh") ;
System.exit(0) ;
}
else if (answer == 1) {
System.exit(1) ;
}
}
Please enlighten me.
______________________________________
Warmest Regards,
Lex
______________________________________
:|: Don't Mess With The Predator :|:
-
Sep 24th, 2005, 09:44 AM
#2
Frenzied Member
Re: Prevent Application Quit
You probably want to code it a little differently. In the windowClosing method, I would simply have System.exit(0);
Somewhere in the program is where you need to prompt the user with the joptionpane, such as the click on a JMenuItem named exit. Then you simply test the values, and if yes is clicked then System.exit(0), if no, then there's no worry, program will continue.
However, if you want to provide some message after,before, during closing then you can take a look at overriding some of these methods:
public void windowClosing(WindowEvent e) { }
public void windowOpened(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
You could possibly put a JOptionPane message dialog in the bold method, but not quit sure about that.
-
Sep 24th, 2005, 04:42 PM
#3
-
Sep 25th, 2005, 02:08 AM
#4
Thread Starter
Member
Re: Prevent Application Quit
Hi,
But if I click "NO", the application still exits. I put the System.exit(1) ; just to exit it properly. But what should i put if i wish to let the application continue to run if the user selects "NO" ?
______________________________________
Warmest Regards,
Lex
______________________________________
:|: Don't Mess With The Predator :|:
-
Sep 25th, 2005, 06:51 AM
#5
Frenzied Member
Re: Prevent Application Quit
You're not understanding the windowClosing method correctly. When that method is reached, then something has ALREADY invoked the frame to close. You can put whatever joptionpane you want there, but the program is already processing a close. You need the code somewhere else. The only thing that would really make sense in those methods are a message dialog, not some prompt.
Let's say if the user clicks an exit button you want to prompt the user if they really want to quit:
Code:
public void actionPerformed(lasjdlfksjaf)
{
if (event.getSource() == exitButton)
{
prompt user
if (yes) System.exit(0); //makes a call to windowclosing
if (no) do nothing
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|