views:

4414

answers:

3

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?

+3  A: 

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

Itay
ok, and what if my click handler is into a different class extarnal to the frame?
Giancarlo
you have to pass a reference to the frame to the click handler (in the constructor)
TofuBeer
+2  A: 

You can call setVisible(false) on the frame.

You might also want to call setDefaultCloseOperation on the frame passing in HIDE_ON_CLOSE (info here: http://java.sun.com/javase/6/docs/api/javax/swing/JFrame.html#setDefaultCloseOperation%28int%29). That will prevent the app from going away if they user hits the "X" on the JFrame to close it.

TofuBeer
A: 

Make sure you do not:

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jedierikb
this is not my problem buddy :)
Giancarlo