I am using windowClosing
to confirm before closing a particular JFrame.
Before closing I get a confirm dialog but the problem is it closes even if I click the NO button. Any help please?
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we)
{
String ObjButtons[] = {"Yes","No"};
int PromptResult = JOptionPane.showOptionDialog(null,
"Are you sure you want to exit?", "Online Examination System",
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null,
ObjButtons,ObjButtons[1]);
if(PromptResult==0)
{
System.exit(0);
}
}
});
What is your JFrame's default close operation set to? You need to make sure that it been set to: jFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Try this
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we)
{
String ObjButtons[] = {"Yes","No"};
int PromptResult = JOptionPane.showOptionDialog(null,"Are you sure you want to exit?","Online Examination System",JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE,null,ObjButtons,ObjButtons[1]);
if(PromptResult==JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With