'jDialog do not dispose

I hava a jFrame that when I press F2 calls a jDialog, in this jDialog, put some information and then when the jButton or Enter is pressed or clicked, I get the information and dispose the jDialog.

However, the jDialog is not closing but getting all the information I request.

Here's the code:

    /**
     * Creates new form DialogCPF
     */
    public DialogCPF(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        jFormattedTextField1.addKeyListener(new Tecla() {
            @Override
            public void keyReleased(KeyEvent e) {
               
            }
        });
    }
 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        
        ConsumirWS2 WS = new ConsumirWS2();
        try {
            WS.Buscar();
        } catch (Exception ex) {
            Logger.getLogger(DialogCPF.class.getName()).log(Level.SEVERE, null, ex);
        }
                jButtonSalvar.setEnabled(true);
                jButtonExcluir.setEnabled(false);
                jButtonINSERIR.setEnabled(false);
                jButtonALTERAR.setEnabled(false);
                jButtonPROCURAR.setEnabled(false);
                jTextFieldAPELIDO.setEnabled(true);
                jTextFieldNOME.setEnabled(true);
                jFormattedTextFieldCPF.setEnabled(true);
                jFormattedTextFieldDATA.setEnabled(true);
                jTextFieldID.setEnabled(false);
                /*DialogCPF dialog = new DialogCPF(new javax.swing.JFrame(), false);
                dialog.dispose();*/

                DialogCPF A = (DialogCPF)evt.getSource();
                A.dispose();
                
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DialogCPF.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DialogCPF.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DialogCPF.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DialogCPF.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DialogCPF dialog = new DialogCPF(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                
                dialog.addKeyListener(new KeyListener(){  

        @Override
        public void keyTyped(KeyEvent e) {
            // Nothing
        }

        @Override
        public void keyPressed(KeyEvent e) {
            // Nothing 
        }

        @Override
        public void keyReleased(KeyEvent e) {
            if(e.getKeyCode() == KeyEvent.VK_ENTER){
        ConsumirWS2 WS = new ConsumirWS2();
        try {
            WS.Buscar();
        } catch (Exception ex) {
            Logger.getLogger(DialogCPF.class.getName()).log(Level.SEVERE, null, ex);
        }
                jButtonSalvar.setEnabled(true);
                jButtonExcluir.setEnabled(false);
                jButtonINSERIR.setEnabled(false);
                jButtonALTERAR.setEnabled(false);
                jButtonPROCURAR.setEnabled(false);
                jTextFieldAPELIDO.setEnabled(true);
                jTextFieldNOME.setEnabled(true);
                jFormattedTextFieldCPF.setEnabled(true);
                jFormattedTextFieldDATA.setEnabled(true);
                jTextFieldID.setEnabled(false);
                DialogCPF dialog = new DialogCPF(new javax.swing.JFrame(), true);
                //dialog.setEnabled(false);
                dialog.dispose();
                }
            }
        });        
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    public static javax.swing.JFormattedTextField jFormattedTextField1;
    private javax.swing.JLabel jLabel1;
    public javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration                   
}

I already tried This, this and this solution, however, I could not manage to bring a solution



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source