Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / May 2005

Tip: Looking for answers? Try searching our database.

destroying swing component

Thread view: 
epicwinter@hotmail.com - 15 May 2005 03:38 GMT
I have an swing app that uses a DesktopPane that has countless
JInternalFrames.  These internal frames are generally just hidden when
they are closed, cause I don't want to recreate them everytime the user
opens them.  I have it set up so they are automatically reinstantiated
if they are null:
if (myInternalFrame =null)
  myInternalFrame = new MyInternalFrame();
myInternalFrame.show();

The user can be re-logged in or changed.  When this happens I want to
destroy all the internal frames, but I can't seem to get it to work.
This is what I have tried.

  JInternalFrame[] frames =  desktopPane.getAllFrames();
       for (int i =0; i< frames.length; i++)
       {
           frames[i].dispose();
       }

But it doesn't seem to destroy the internal frames but rather just
remove them from the desktop pane and close them.

I thought after the frames[i].dispose() it would make the internal
frame null but it doesn't.  It just seems to dereference it from the
desktop pane.  I tried explicitly calling frames[i]=null but that just
nulls the reference.  Is there a way to actually destroy the frame. So
when i reference if (myInternalFrame =null) it is actually null?

thanks
Michael Rauscher - 15 May 2005 13:14 GMT
Hi,

[...]
> This is what I have tried.
>
[quoted text clipped - 6 lines]
> But it doesn't seem to destroy the internal frames but rather just
> remove them from the desktop pane and close them.

I can't figure out the part, where the API states that internal frames
will be destroyed by calling dispose:

<api>
public void dispose()

    Makes this internal frame invisible, unselected, and closed. If the
frame is not already closed, this method fires an INTERNAL_FRAME_CLOSED
event. The results of invoking this method are similar to
setClosed(true), but dispose always succeeds in closing the internal
frame and does not fire an INTERNAL_FRAME_CLOSING event.
</api>

Also JFrame#dispose doesn't destroy the object. It just releases native
resources. JInternalFrame objects are lightweight components - they
don't allocate native ressources.

> I thought after the frames[i].dispose() it would make the internal
> frame null but it doesn't.  It just seems to dereference it from the
> desktop pane.  I tried explicitly calling frames[i]=null but that just
> nulls the reference.  Is there a way to actually destroy the frame. So
> when i reference if (myInternalFrame =null) it is actually null?

There's no way to explicitly destroy objects in Java. This is the task
of the GC. Just don't hold any references to your internal frame object.
The GC will do the rest.

Bye
Michael
epicwinter@hotmail.com - 15 May 2005 19:01 GMT
> Hi,
>
[quoted text clipped - 12 lines]
> I can't figure out the part, where the API states that internal frames
> will be destroyed by calling dispose:

I read the documentation.  I am well aware that dispose isn't the
answer, that is why I am posing this question.

> <api>
> public void dispose()
[quoted text clipped - 22 lines]
> Bye
> Michael

I need to hold references to these objects because swing components can
be slow to generate and I open certain frames over and over.  So I
don't want to reinstantiate these components every time I need to
display them.
Andrey Kuznetsov - 15 May 2005 19:15 GMT
> I need to hold references to these objects because swing components can
> be slow to generate and I open certain frames over and over.  So I
> don't want to reinstantiate these components every time I need to
> display them.

you contradicts a bit to yourself.
solution is simple, don't destroy Objects "every time", but only on user
login/logout
- i.e. if user is changed you should set to null all references to your
JInternalFrame(s).

Signature

Andrey Kuznetsov
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

epicwinter@hotmail.com - 15 May 2005 20:21 GMT
> > I need to hold references to these objects because swing components can
> > be slow to generate and I open certain frames over and over.  So I
[quoted text clipped - 12 lines]
> http://reader.imagero.com Java image reader
> http://jgui.imagero.com Java GUI components and utilities

That is exactly how I am trying to set it up.  But I am trying to do
this from the desktop pane.  Like so:
  JInternalFrame[] frames =  desktopPane.getAllFrames();
       for (int i =0; i< frames.length; i++)
           {
         //do something here to destroy frames[i]
           }
Michael Rauscher - 15 May 2005 23:30 GMT
epicwinter@hotmail.com schrieb:

> That is exactly how I am trying to set it up.  But I am trying to do
> this from the desktop pane.  Like so:
[quoted text clipped - 3 lines]
>           //do something here to destroy frames[i]
>             }

Again, you *can not* destroy objects in Java. It's simply not possible.
It's task of the garbage collector to do this. The GC destroys all
unused objects (an object is unused if there are no more references to it).

I don't know neither your code nor how many references to your internal
frames exist and where these references are. So it's impossible to
insert a line of code in the above snippet that has general validity.

Anyway you have to remove the internal frames from the desktop (see
Container#remove, Container#removeAll) since the the desktop holds
references to them.

But this doesn't mean, that objects removed from the desktop are then
unused. Your objects may hold additional references to them, causing
them not to be garbage collected.

Bye
Michael
mkrause - 16 May 2005 21:20 GMT
It looks like your problem is that you still are holding references to
the frames you have disposed of.  In order for them to be garbage
collected, they need to be unreachable (e.g., you don't hold any
references to them) from your program.

I would check to make sure you do not have other things that reference
your frames (things like a member variable collection or a keyboard,
mouse, or focus listener, etc.)  Anything that is reachable that
maintains a reference to one of your internal frames will prevent it
from being garbage collected.

Lastly, I came across a bug that may also cause a problem.  The
details:

Java Bug Id 478141
Synopsis  First JInternalFrame added to a JDesktop causes memory leak
http://developer.java.sun.com/developer/bugParade/bugs/4748141.html

Something I have made a point to do if I require stateful setup and
cleanup of my swing components is to subclass them (or wrap in a
delegate class) where you know that the setup and cleanup will always
get called.  As Michael Rauscher mentioned below, you can't count on
when or if the JVM will clean up your objects.  So, be careful, because
even the tiniest reference from a listener can hold a BIG chunk of
memory that you weren't aware of.

I'd recommend checking out a good memory debugging utility.  I've used
JProbe which has helped catch a lot of loitering objects.

- Mike
epicwinter@hotmail.com - 18 May 2005 03:02 GMT
> It looks like your problem is that you still are holding references to
> the frames you have disposed of.  In order for them to be garbage
[quoted text clipped - 26 lines]
>
> - Mike

Thanks mike some good thoughts here.  Yeah I think i just need to have
a more organized method of storing my references and manage them from
there.  Lots of code to change...
epicwinter@hotmail.com - 20 May 2005 03:11 GMT
By the way this is why I am having a problem when you add a component
to a container this gets executed:
        System.arraycopy(component, index, component,
                index + 1, ncomponents - index);

Instead of keeping the original reference it copies it.  And so when I
get all frames I can't destroy the original reference which is really
what I am asking.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.