Possible Duplicate:
How to programmatically close a JFrame
I am developing a java GUI using JFrame. I want to close the GUI frame and dispose it off through code. I have implemented :
topFrame.addWindowListener(new WindowListener()
{
public void windowClosing(WindowEvent e)
{
emsClient.close();
}
public void windowOpened(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowActivated(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
});`
How can I invoke the windowClosing event?? Or is there some other way?
We can also close the window by clicking on the cross button X. While clicking the X button at the top right corner, the JFrame window programmatically is sent to the window closing event.
You can use setVisible(false) on your JFrame if you want to display the same frame again. Otherwise call dispose() to remove all of the native screen resources.
This will programmatically trigger the window closing event:
topFrame.dispatchEvent(new WindowEvent(topFrame, WindowEvent.WINDOW_CLOSING));
If you want to close the frame you need to call:
topFrame.dispose();
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