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?
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 anullcheck and recreate the frame as required