Question:
In the code below, I want that when choosing sim
the window will close and when choosing não
the window will remain open.
private void formWindowClosing(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
int Confirm = JOptionPane.showConfirmDialog(null,"Encerrar?","sim ou nao", JOptionPane.YES_NO_OPTION);
if (Confirm == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(null, "");
System.exit(0);
} else if (Confirm == JOptionPane.NO_OPTION){
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
It doesn't give any error, the problem is that when choosing sim
the window closes. So far correct, but when choosing não
it closes in the same way.
Answer:
You have to use YES_NO_OPTION, try the code snippet below, which I got from an old project of mine. Any questions, just return. Hug
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
if (JOptionPane.showConfirmDialog(frame,
"Titulo", "Tem certeza ?",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION){
System.exit(0);
}
}
});