Fam
Fam

Reputation: 718

"Kill" an open JFrame

I have a class that extends JFrame that represents a dialog window for my program. This dialog accepts user input through a text field. Depending on the input, the window may also update with a different message (stored as a JLabel), if need be.

I noticed that when I click on a button in the dialog (which in my code, disposes the dialog), if I activate the dialog again (by selecting the control that initiates the dialog in the first place), the text I previously entered, and also the state of the JLabel remains. I am looking for a way to "kill" the running instance of the dialog when it disappears, so when it does appear again, it is "new," with a blank text field and the default JLabel. Is this possible?

Upvotes: 0

Views: 2457

Answers (3)

syb0rg
syb0rg

Reputation: 8247

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

This will close only one window if you have multiple windows with your program open. When only one window is left, and it is closed, the program stops.

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347334

It sounds like you are maintaining a reference to the frame.

Instead, you may want to use JFrame#setDefaultCloseOperation(JFrame.DISPOSE) to dispose of the frames underlying native resources and re-create it again when you need it.

Updated

When you are done with the frame in you main code, simply settle variable to null. In your code that displays the frame, you would need to do a null check and recreate the frame as required

Upvotes: 2

Aniket Inge
Aniket Inge

Reputation: 25723

did you try to set JFrame#setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)??

Upvotes: 0

Related Questions