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.
Re: Prevent Application Quit
If you choose 1 then you don't want to do anything, right? Why add the exit code there? You probably don't need the elseif at all if I understand correctly :
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) ;
}
}
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" ?
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
}
}