Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java swing close window without exiting app

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?

like image 770
Giancarlo Avatar asked Feb 21 '09 16:02

Giancarlo


People also ask

How do I close a JFrame without exiting the application?

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.

How do you close a frame in a swing?

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).

Which method is used to close a swing frame in Java?

We can close the AWT Window or Frame by calling dispose() or System. exit() inside windowClosing() method.


2 Answers

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

like image 59
Itay Maman Avatar answered Sep 22 '22 15:09

Itay Maman


Maybe a cleaner way is just change the setDefaultCloseOperation from EXIT_ON_CLOSE to DISPOSE_ON_CLOSE :

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
like image 29
aleroot Avatar answered Sep 21 '22 15:09

aleroot