User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 427,228 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,196 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 454 | Replies: 10
Reply
Join Date: May 2008
Posts: 44
Reputation: TheWhite is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
TheWhite TheWhite is offline Offline
Light Poster

setting the frame = to null on close?

  #1  
Jul 11th, 2008
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?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2006
Posts: 1,460
Reputation: masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice 
Rep Power: 9
Solved Threads: 131
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Virtuoso

Re: setting the frame = to null on close?

  #2  
Jul 11th, 2008
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).
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: May 2008
Posts: 44
Reputation: TheWhite is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
TheWhite TheWhite is offline Offline
Light Poster

Re: setting the frame = to null on close?

  #3  
Jul 11th, 2008
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...
Reply With Quote  
Join Date: Feb 2006
Posts: 1,460
Reputation: masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice 
Rep Power: 9
Solved Threads: 131
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Virtuoso

Re: setting the frame = to null on close?

  #4  
Jul 11th, 2008
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.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: May 2008
Posts: 44
Reputation: TheWhite is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
TheWhite TheWhite is offline Offline
Light Poster

Re: setting the frame = to null on close?

  #5  
Jul 11th, 2008
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...
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,847
Reputation: Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all 
Rep Power: 12
Solved Threads: 283
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Maven

Re: setting the frame = to null on close?

  #6  
Jul 11th, 2008
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.
Reply With Quote  
Join Date: Feb 2006
Posts: 1,460
Reputation: masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice 
Rep Power: 9
Solved Threads: 131
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Virtuoso

Re: setting the frame = to null on close?

  #7  
Jul 11th, 2008
Originally Posted by Ezzaral View Post
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. ;-)
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: May 2008
Posts: 44
Reputation: TheWhite is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
TheWhite TheWhite is offline Offline
Light Poster

Re: setting the frame = to null on close?

  #8  
Jul 11th, 2008
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?
Last edited by TheWhite : Jul 11th, 2008 at 8:18 pm.
Reply With Quote  
Join Date: Jun 2008
Location: WA, USA
Posts: 778
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Rep Power: 4
Solved Threads: 76
Alex Edwards's Avatar
Alex Edwards Alex Edwards is online now Online
Master Poster

Re: setting the frame = to null on close?

  #9  
Jul 11th, 2008
Originally Posted by TheWhite View Post
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--

  1.  
  2. boolean isDisposed = false;
  3.  

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

  1.  
  2. @Override
  3. public void dispose(){
  4. isDisposed = true;
  5. super.dispose();
  6. }
  7.  

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

  1.  
  2. public boolean disposed(){
  3. return isDisposed;
  4. }
  5.  

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

  1. if(frame2.disposed() == true)

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

  1.  
  2. private MyFrame mf;
  3.  

instead of...

  1.  
  2. private Frame f;
  3. // or JFrame or whatever container type you're using--
  4.  
Last edited by Alex Edwards : Jul 11th, 2008 at 8:32 pm.
Reply With Quote  
Join Date: May 2008
Posts: 44
Reputation: TheWhite is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
TheWhite TheWhite is offline Offline
Light Poster

Re: setting the frame = to null on close?

  #10  
Jul 11th, 2008
I wasn't aware the variables could be accessed if the frame was disposed...... But that's a good idea...
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 11:39 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC