What's the programmatic equivalent of clicking the close (x) button in the upper right corner of a JFrame?
There's the dispose()
method but that's not the same thing, since a JFrame can be set to do several different things upon closing (not to mention if there's a WindowListener involved)
We can close the AWT Window or Frame by calling dispose() or System. exit() inside windowClosing() method. The windowClosing() method is found in WindowListener interface and WindowAdapter class.
The method JFrame. setVisible can be used to hide or display the JFrame based on the arguments, while JFrame. dispose will actually "destroy" the frame, by closing it and freeing up resources that it used.
Use anything for setDefaultCloseOperation excepting EXIT_ON_CLOSE . Then, for the cancel button, just dispose() the window. If the application has another running thread, it won't exit. You can also hide a window by calling setVisible(false) .
You tell the component to dispatch an event. In this case, you want it do dispatch a Window Closing event.
private void exit() {
this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
}
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