This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

closing a frame  RSS feed

 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi:
I have a ParentFrame with some buttons. One of the buttons,
creates and brings up ChildForm. Now how do I close the
ChildForm, without using a System.exit(1).
I was thinking of the dispose method, but the more I think(?)
the more confused I am.
Could someone give me a conceptual idea....okay I can provide
a close button on the ChildForm. Now what should my CloseButton.mousePressed() do?
thanks.
- satya
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The dispose method is the correct one to choose. It will remove the window and free the memory it used:
newWindow.dispose();
Regards,
Manfred.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

so how did you get the ref to newWindow (in my case ChildForm) in the mousePressed()
listener. thats my confusion.
regds.
- satya
 
deekasha gunwant
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi madhav,
may be you can store the reference to the child Frame when u are creating it, and then later use this reference to dispose it.
regards
deeksha
 
Suneel Setlur
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
By childForm, do you mean another Frame which you are creating ?
If yes, you are making it visible by saying setVisible(true).
You can deipose it by also saying setVisible(false);
Suneel
p.s: Its still available in memory.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suneel:
p.s: Its still available in memory
thats what I am talking abt, I don't want to waste
mem. Say the user hits the CANCEL button, do you want
it to be in memory? Defenitely NOT!
deekasha:
may be you can store the reference to the child Frame when u are creating it, and then later use this
reference to dispose it

what I found is this:
from the event, getSource() gets a ref to the button, on which I can do a getParent()
which will get me the Frame. Hence I can call the
dispose() on the Frame.
I discovered this late yesterday and was thinking of posting
it today.
However, after I call dispose(), I am not sure if the reference
to the Frame also is removed. The API doc says "this will
release all the native resources" or something close.
So, am I safe in assuming that the ref to the ChildFrame is
removed and it will eventually be garbage collected?
Thats' what is holding me up.

Thanks for your response.
regds.
- satya
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Madhav,
The documentation on the dispose() method is pretty confusing, but I believe the operative words in the documentation is native resources. All the AWT components are defined as "heavy-weight" (as opposed to "light-weight" Swing components) because they are all based off of native code... Thus, I would think that the dispose() method destroys the native resources only, but keeps the reference to these components in the JVM, thus they will not be garbage collected... the documentation goes on to state that the component that was disposed can be made displayable again with a call to pack() or show(), which rebuilds all the native resources. All the references are still in the JVM, but their native resources are gone.
HTH,
-Nate
 
dave taubler
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want the child Frame to be garbage collected, just set its reference in the parent Frame to null. i.e., put this code in your parent frame:
<pre>
ChildFrame cf = new ChildFrame();
cf.setVisible(true);
cf = null;
</pre>
Or, if you want to keep your cf reference as long as the child is open,create a method like this in your parent frame, and then call it from your child frame as it closes:
<pre>
public void childClosing() {
cf = null;
}
</pre>
Of course, your child frame will need a reference back to the parent frame to do this. Something like this:
<pre>
private ParentFrame parent;
...
public setParentFrame(ParentFrame pf) {
parent = pf;
}
...
this.addWindowListener(new WindowAdapter() {
public void WindowClosing(WindowEvent we) {
parent.childClosing();
this.dispose();
this.setVisible(false);
}
}
</pre>
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!