I have a little frame where I ask user & password. This frame will be opened clicking over a button in a main window.
Then I have two buttons: ok and cancel.
When I click on "cancel" button, I need to close this frame without exiting the app.
How can I do that?
DISPOSE_ON_CLOSE -- The frame will be closed and disposed but the application will not exit. JFrame. DO_NOTHING_ON_CLOSE -- The frame will be closed but not disposed and the application will not exit.
You can easily close your JFrame by clicking on the X(cross) in the upper right corner of the JFrame. However JFrame. setDefaultCloseOperation(int) is a method provided by JFrame class, you can set the operation that will happen when the user clicks the X(cross).
We can close the AWT Window or Frame by calling dispose() or System. exit() inside windowClosing() method.
You can use either Frame.hide() or Frame.dispose(). I would also recommend to look into JDialog or JOptionPane
Correction: hide() is deprecated. SetVisible(false) should be used instead
Maybe a cleaner way is just change the setDefaultCloseOperation from EXIT_ON_CLOSE to DISPOSE_ON_CLOSE :
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
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