How to make a decision structure to close a window?

Asked

Viewed 3,039 times

3

In the code below, I want you to choose sim the window closes and when choosing não the window remains 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);

    }
}

There is no mistake, the problem is that when choosing sim window closes. So far correct, but when choosing não it closes the same way.

  • What a mistake you made in your attempt?

  • no error... the problem is that when choosing yes the window closes.. up to right... more when choosing not it closes in the best way..

  • Vlw guys more already worked akie.. OBG..

  • 1

    Netobass, you can publish the solution as a response below?

  • if (JOptionPane.showConfirmDialog(null, "Encerrar?","sim ou nao", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
} else {
 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE)
}

  • as I do not have 10 points still akie on the forum so I can not define the answer yet..

  • 1

    @Netobass can. You don’t need points to leave an answer. Search below Publique sua resposta

Show 2 more comments

3 answers

1

You have to use YES_NO_OPTION, try the code snippet below, which I took 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);
            }
        }
    });
  • Sure you used this in an old project of yours? Did you put the title in the place of the body of the message and the message in the place of the title? Another thing, he is already using YES_NO_OPTION.

0

I decided to change in its last line of code the part "DISPOSE_ON_CLOSE" for "DO_NOTHING_ON_CLOSE" being like this:

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 

0


I create a private method(Private void) Confirm output, and then just invoke it every time you need to use formWindowClosing or Keypressed Events sometimes, so your system saves code and gets smaller and easier to understand =) .

private void ConfirmarSaida(){

        JOptionPane sair = new JOptionPane();
        int Sair = sair.showConfirmDialog(null,"Você deseja sair do programa?","Sair",JOptionPane.YES_NO_OPTION);
      if(Sair == JOptionPane.YES_OPTION){
       //    System.exit(0);
       this.dispose();

      }else{
         if(Sair == JOptionPane.NO_OPTION){

         }
      }


}

Ah! and do not forget in the configuration of Jframe or Jinternalframe or whatever to change, the Setdefaultcloseoperation for DO_NOTHING(do nothing), so when you click on the X of the window it will only use its Joptionpane, if you leave DISPOSE or EXIT_ON_CLOSE in Setdefaultcloseoperation, it will close even if you click not in Joptionpane on the quit condition.

hope to help in something.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.