Close System after Dialog Close

Asked

Viewed 190 times

1

How can I make a login screen when it is closed, also close the whole system?

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

I tried to change the code of setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE) for setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE) but the system presents me with 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);
    }
}
  • See the two questions marked as duplicates. I believe they have the solution to your doubt.

  • Thanks for the return, however I can not set EXIT_ON_CLOSE in a Jdialog, because it is a login modal, need that when closing the modal, the whole system closes (unless login is done).

  • Ernani, present a [mcve] of your code so that we can execute, reopen and suggest something better. Without an example that anyone can execute has no way to help you.

  • Ernani your code does not reflect the question problem. Where is the login screen? Why login validation should close everything if it does not occur correctly? This example has virtually nothing to do with doubt.

  • The issue is not user validation, it is about the user clicking on the "X" of the Dialog tab and the whole system not being closed together, even if Jdialog set as EXIT_ON_CLOSE

1 answer

0


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

        addWindowListener(new WindowAdapter() {

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

Working:

inserir a descrição da imagem aqui

Browser other questions tagged

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