Is there a way to make a frame totally wipe all traces of itself (make it return null) by pressing the X on the window?

setDefaultCloseOperation(DISPOSE_ON_CLOSE);

doesn't actually make the frame return null, rather, it saves its state and cleans it out of memory... A call to show or pack will bring it back...... I want it gone!

Any ideas?

Recommended Answers

All 10 Replies

So don't call setVisible(true) (show is deprecated).

Really, as long as your frame isn't a instance variable (or even worse class variable) in a class that is always active, it will, eventually, be garbage collected as soon as it is has gone out of scope and been disposed. If you feel you absolutely must "kill" it, then you can't simply use the "defaultCloseOption", you have to write your own WindowListener and "kill" it in the windowClosing/windowClosed method (if you want the event model to do it for you).

Well, I have my main program in a JFrame and then another class that extends JFrame in a totally different .java file..

In the main program, I can set the 2nd JFrame = to null any time which does what i want, BUT pressing the X on the frame can either hide it, dispose it (which doesn't set it to null), or do nothing.....

You would think calling dispose() on the frame would simply set it to null and have it garbage collected, but it doesnt... Actually, I'm not so sure what it does...

Right, it doesn't. It does destroy the all of the event threads references to it, but if you still hold one, it will still exist. The event thread cannot do anything about the references you hold.

Like I said, you will have to write your own WindowListener.

So what can I put in the windowListener's windowClosing() method to make it totally get rid of the entire instance?

The problem is, I want the class to be self sustaining... I don't want to have to overwrite the windowListener through my main program just so I can set it equal to null that way...

I want to call a method, that does more than dispose() which kills everything, setting it to null...

Why are you so concerned that it's not explicitly null? dispose() is sufficient for all practical purposes that I can think of

Releases all of the native screen resources used by this Window, its subcomponents, and all of its owned children. That is, the resources for these Components will be destroyed, any memory they consume will be returned to the OS, and they will be marked as undisplayable.

Why are you so concerned that it's not explicitly null? dispose() is sufficient for all practical purposes that I can think of

Yes, it is, but sometimes it's just not worth it to argue the point. ;-)

I believe he doesn't realise that to call pack and setVisible(true) on the reference again, will simply "rebuild" the frame. ;-)

Because I have other code in my program that checks if(frame2 == null)... and having the X dispose() does not make it = null.. It just hides it there until you bring it up again...

Is there a way to ask if a frame has been disposed because I could use that instead...

The most important thing is to have the frame not take up memory while it's not in use and to have a method of finding out when the frame is available and not available... To me, the best option sounded like setting it = to null...

See where I'm getting at?

Because I have other code in my program that checks if(frame2 == null)... and having the X dispose() does not make it = null.. It just hides it there until you bring it up again...

Is there a way to ask if a frame has been disposed because I could use that instead...

The most important thing is to have the frame not take up memory while it's not in use and to have a method of finding out when the frame is available and not available... To me, the best option sounded like setting it = to null...

See where I'm getting at?

If you're modifying the frame through an extension, why not make your frame have a boolean data type to determine if it has been disposed of or not--

boolean isDisposed = false;

--and override the method dispose such that when called it will set the boolean to true--

@Override
public void dispose(){
     isDisposed = true;
     super.dispose();       
}

it may also help to have this data type returnable--

public boolean disposed(){
     return isDisposed;
}

--with this you don't lose the functionality of dispose and have a way of determining if dispose has been called.

Now you can use this for your condition--

if(frame2.disposed() == true)

Edit: You'll obviously have to use extended frames for your references instead of the frame itself. Example--

private MyFrame mf;

instead of...

private Frame f;
// or JFrame or whatever container type you're using--

I wasn't aware the variables could be accessed if the frame was disposed...... But that's a good idea...

I believe that you can call isDisplayable() on the frame to determine if it has been disposed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.