java – How to make a decision structure to close a window?


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, "");
    } else if (Confirm == JOptionPane.NO_OPTION){


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.


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() {
        public void windowClosing(java.awt.event.WindowEvent windowEvent) {
            if (JOptionPane.showConfirmDialog(frame, 
                "Titulo", "Tem certeza ?", 
                JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION){
Scroll to Top