How do I know if a JDialog window is open or closed?

2

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

I'm going to use this "information" as a parameter in a condition, for example, if closed screen does such 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 .."));
    }
}
    
asked by anonymous 16.09.2017 / 22:55

1 answer

4

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

    
16.09.2017 / 23:03