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?
JFrame.dispose() public void dispose() Releases all of the native screen resources used by this Window, its subcomponents, and all of its owned children. That is, the resources for these Components will be destroyed, any memory they consume will be returned to the OS, and they will be marked as undisplayable.
Oracle will continue developing Swing and AWT in Java SE 8 and Java SE 11 (18.9 LTS). This means they will be supported by Oracle through at least 2026.
The Dispose() method The Dispose method performs all object cleanup, so the garbage collector no longer needs to call the objects' Object.
A Frame is an AWT component whereas a JFrame is a Swing component.
Although you should probably avoid the use of multiple JFrame
s, isDisplayable()
is a method you could use for this.
Example:
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();
}
});
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With