this.dispose () in Eclipse does not work

0

When I write:

this.dispose();

Using Eclipse with Windows Builder, it does not work for me, does not close the current window, what can I do?

Code:

package ventanas;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import datos.Dato_login;
import javafx.scene.Parent;
import logicas.Logica_login;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import javax.swing.JCheckBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Ventana_login extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = -7948060398287723741L;
    private JPanel contentPane;
    private JTextField txtUsuario;
    private JPasswordField txtContrasena;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Ventana_login frame = new Ventana_login();
                    frame.setVisible(true);

                    // Centrar el Frame en la pantalla
                    frame.setLocationRelativeTo(null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Ventana_login() {
        setTitle("Sistema Gestor de Eventos v1.0");
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblBienvenidoAlSistema = new JLabel("Bienvenido");
        lblBienvenidoAlSistema.setHorizontalAlignment(SwingConstants.CENTER);
        lblBienvenidoAlSistema.setFont(new Font("Tahoma", Font.BOLD, 16));
        lblBienvenidoAlSistema.setBounds(10, 11, 424, 14);
        contentPane.add(lblBienvenidoAlSistema);

        JLabel lblUsuario = new JLabel("Usuario");
        lblUsuario.setHorizontalAlignment(SwingConstants.RIGHT);
        lblUsuario.setBounds(96, 79, 70, 14);
        contentPane.add(lblUsuario);

        JLabel lblContrasena = new JLabel("Contrase\u00F1a");
        lblContrasena.setHorizontalAlignment(SwingConstants.RIGHT);
        lblContrasena.setBounds(96, 109, 70, 14);
        contentPane.add(lblContrasena);

        txtUsuario = new JTextField();
        txtUsuario.setBounds(176, 76, 150, 20);
        contentPane.add(txtUsuario);
        txtUsuario.setColumns(10);

        JButton btnIniciarSesion = new JButton("Iniciar Sesi\u00F3n");
        btnIniciarSesion.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
                    Dato_login d_lgn = new Dato_login();
                    Logica_login l_lgn = new Logica_login();

                    d_lgn.setUsuario(txtUsuario.getText());
                    char[] contrasenaChar = txtContrasena.getPassword();
                    String contrasenaClean = new String(contrasenaChar);
                    d_lgn.setContrasena(contrasenaClean);

                    Dato_login d_lgn2 = l_lgn.login(d_lgn.getUsuario(), d_lgn.getContrasena());

                    if (Logica_login.resultado) {
                        Ventana_menu v_menu = new Ventana_menu();
                        v_menu.setVisible(true);
                        v_menu.setLocationRelativeTo(null);
                        Ventana_menu.lblPerfilActual.setText(d_lgn2.getPerfil());
                        Ventana_menu.lblApellidoActual.setText(d_lgn2.getApellido());
                        Ventana_menu.lblNombreActual.setText(d_lgn2.getNombre());
                        Ventana_menu.lblUsuarioActual.setText(d_lgn2.getUsuario());

                        if (Ventana_menu.lblPerfilActual.getText().equals("Portero")) {
                            Ventana_menu.btnMantenimiento_Eventos.setEnabled(false);
                            Ventana_menu.btnMantenimiento_Invitaciones.setEnabled(false);
                            Ventana_menu.btnMantenimiento_Invitados.setEnabled(false);
                            Ventana_menu.btnMantenimiento_Usuarios.setEnabled(false);
                            Ventana_menu.btnReportes.setEnabled(true);
                        } else {
                            Ventana_menu.btnMantenimiento_Eventos.setEnabled(true);
                            Ventana_menu.btnMantenimiento_Invitaciones.setEnabled(true);
                            Ventana_menu.btnMantenimiento_Invitados.setEnabled(true);
                            Ventana_menu.btnMantenimiento_Usuarios.setEnabled(true);
                            Ventana_menu.btnReportes.setEnabled(true);
                        }
                        // DISPOSE
                        this.dispose();
                    } else {
                        JOptionPane.showMessageDialog(contentPane, "Acceso Denegado", "Error", JOptionPane.ERROR_MESSAGE);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    JOptionPane.showMessageDialog(null, "Exception:\n" + e, "Error: Ventana_login", JOptionPane.ERROR_MESSAGE);
                    }
            }
        });
        btnIniciarSesion.setBounds(176, 163, 150, 30);
        contentPane.add(btnIniciarSesion);

        JLabel lblNewLabel = new JLabel("2016 \u00A9 PUCMM - Programaci\u00F3n I");
        lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 9));
        lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        lblNewLabel.setBounds(10, 246, 424, 14);
        contentPane.add(lblNewLabel);

        txtContrasena = new JPasswordField();
        txtContrasena.setBounds(176, 106, 150, 20);
        contentPane.add(txtContrasena);

        JCheckBox chckbxMostrarContrasena = new JCheckBox("Mostrar contrase\u00F1a");
        chckbxMostrarContrasena.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if (chckbxMostrarContrasena.isSelected()) {
                    txtContrasena.setEchoChar((char)0);
                } else {
                    txtContrasena.setEchoChar('\u2022');
                }
            }
        });
        chckbxMostrarContrasena.setHorizontalAlignment(SwingConstants.RIGHT);
        chckbxMostrarContrasena.setBounds(176, 133, 150, 23);
        contentPane.add(chckbxMostrarContrasena);

        JLabel lblSistemaGestorDe = new JLabel("Sistema Gestor De Eventos");
        lblSistemaGestorDe.setHorizontalAlignment(SwingConstants.CENTER);
        lblSistemaGestorDe.setFont(new Font("Tahoma", Font.BOLD, 16));
        lblSistemaGestorDe.setBounds(10, 36, 424, 14);
        contentPane.add(lblSistemaGestorDe);
    }
}
    
asked by Robert Gomez 03.12.2016 в 08:05
source

2 answers

1

It may be due to two different failures:

Failed using 'this'

When you use this.dispose() you have to be careful what this is. Look closely at the area in which you are calling to make sure that it really is what you want to close.

Failed with another item

Java documentation (Window.dispose ()) says that it releases all of its own resources and its subcomponents, but also says:

  

The Window and its subcomponents can be made displayable again by rebuilding the native resources with a subsequent call to pack or show.

It means that the window can be shown again if "another element" does it and it should be doing it even if you see that the code keeps advancing until leaving the main .

Gross solution

If you really want to kill the application after calling dispose() you can call System.exit(1) . That closes the execution of the JVM.

    
answered by 03.12.2016 / 10:36
source
0

Add your code, but you can try to send it from your JFrame instance:

jframe.dispose();
    
answered by 03.12.2016 в 08:14