Close system after closing Dialog

1

How can I make a login screen when closing, also close the entire system?

In the code below I have a main screen with the "change user" button, when clicking, the system opens a Dialog for the new user to log in, however if the user closes Dialog , it continues with access to the system and I would like that when closing Dialog , the whole system is closed.

I tried to change the code from setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE) to setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE) but the system gives me an error:

  

"Exception in thread" AWT-EventQueue-0 "   java.lang.IllegalArgumentException: defaultCloseOperation must be one   of: DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE, or DISPOSE_ON_CLOSE ".

Main Screen Code

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class TPrincipal extends JFrame {

    private TPrincipal getInstance() {
        return this;
    }

    public TPrincipal() {
        setTitle("Frame principal");
        setSize(400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton btnFrame = new JButton("Mudar Usuário");
        btnFrame.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //passando a instancia do Frame para referencia do modal
                new TLogin(getInstance()).start();
            }

        });
        setLayout(new BorderLayout());
        add(new JLabel("Este é o frame principal"), BorderLayout.CENTER);
        add(btnFrame, BorderLayout.PAGE_END);
        setVisible(true);
        setLocationRelativeTo(null);
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TPrincipal().setVisible(true);
            }
        });
    }
}

Login Screen Code

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;

class TLogin extends JDialog {

    public TLogin(TPrincipal owner) {
        super(owner, "Dialog 02", true);
    }

    public void start() {
        add(new JLabel("Esta é a segunda janela modal"));
        JButton btnFrame = new JButton("LOGIN REALIZADO - Fechar apenas modal");
        btnFrame.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                //login realizado com sucesso
            }
        });
        setSize(300, 300);
        add(btnFrame, BorderLayout.PAGE_END);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(getParent());
        setVisible(true);
    }
}
    
asked by anonymous 13.05.2018 / 05:43

1 answer

1

To force the application to close when the modal window is closed in the x window, you can add a WindowListener and overwrite the windowClosed() method, which is called when trying to close in this way.

        addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

Running:

    
13.05.2018 / 22:45