How to know if a Jdialog window is open or closed?

Asked

Viewed 331 times

2

I was wondering, how can I know if a screen, in my case a JDialog is open. Is there any event or way to know this?

Note: I will use this "information" as a parameter in a condition, for example, if closed screen does such a thing.

import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class FecharTela extends JFrame {

    private JButton botao = new JButton("+");
    private DialogX x = new DialogX();

    public FecharTela() {
        setTitle("Teste");
        add(montaTela());
        setSize(500, 400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private JComponent montaTela() {
        JPanel jpMontaTela = new JPanel();
        jpMontaTela.add(botao);

        botao.addActionListener((ActionEvent e) -> {

            x.setVisible(true);
        });

        return jpMontaTela;
    }

    public static void main(String[] args) {
        FecharTela ft = new FecharTela();
        ft.setVisible(true);
    }
}

class DialogX extends JDialog {

    public DialogX() {
        setSize(300, 300);
        setLocationRelativeTo(null);
        add(new JLabel("Erro no campo .."));
    }
}

1 answer

4


You can use the methods isVisible() and/or isDisplayable() inherited from the class Component to check whether the window has already been opened or not.

Browser other questions tagged

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