10

I've got an application with several JFrame window.

My main JFrame wants to know if another JFrame is already disposed.

Which method shall I call to decide?

Jad Chahine
  • 6,849
  • 8
  • 37
  • 59
principal-ideal-domain
  • 3,998
  • 8
  • 36
  • 73

2 Answers2

16

Although you should probably avoid the use of multiple JFrames, isDisplayable() is a method you could use for this.

Example:

enter image description here

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Example {

    public Example() {
        JFrame frame = new JFrame("Frame 1");
        JFrame frame2 = new JFrame("Frame 2");

        JLabel label = new JLabel("");

        JButton button = new JButton("Check");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                label.setText(frame2.isDisplayable() ? "Active" : "Disposed");
            }
        });

        JPanel panel = new JPanel();
        panel.add(button);
        panel.add(label);

        frame.setContentPane(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200, 100);
        frame.setVisible(true);

        frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame2.setSize(200, 100);
        frame2.setLocation(frame.getX() + frame.getWidth(), frame.getY());
        frame2.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Example();
            }
        });
    }
}
Community
  • 1
  • 1
Lukas Rotter
  • 4,158
  • 1
  • 15
  • 35
0

You will want to use a WindowListener to check if it is disposed.

liquidsystem
  • 634
  • 5
  • 15