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 / February 2005

Tip: Looking for answers? Try searching our database.

Correct "Dispose" of junk JPanel (not JFrame)

Thread view: 
P.Hill - 02 Feb 2005 22:36 GMT
I haven't done too much Swing programming in a while and I didnt
normally throw away panels within Frames, so was wondering
if someone had the right answer for the following.

I have 1 frame that contains several panels.
I want to replace a panel of buttons,
do I have to remove listeners before removing the panel
in order to free all memory do I

Currently all I have is:
        if ( buttonPanel != null ) {
            frame.getContentPane().remove(buttonPanel);
        }

The buttonPanel is created with code like:
    ...
        buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(row, col));

        for (Iterator i = moves.iterator(); i.hasNext();) {
        ...
            Button button = new Button( ... );
            button.addActionListener( new Doer( ... );
            buttonPanel.add(button);
        }
        frame.getContentPane().add(BorderLayout.SOUTH, buttonPanel);

Note, the "Doer" is an inner class that only refers to the outer class
(the controller class containing the above code) and any appropriate
parts of the model class.

Is the correct way in JDK 1.4 or beyond require anything more than the
above remove() before I start building another button panel?

A JDK 1.4 reference on how to what to do with components when you
are done with them would be good.  I couldn't find a useful page
on the subject. Here is a recent page which seems to suggest that just
the remove is fine:
http://forum.java.sun.com/thread.jspa?messageID=3021730

thanks,
-Paul
Larry Barowski - 03 Feb 2005 00:44 GMT
> I haven't done too much Swing programming in a while and I didnt
> normally throw away panels within Frames, so was wondering
[quoted text clipped - 4 lines]
> do I have to remove listeners before removing the panel
> in order to free all memory do I

No, those listeners are references from the mess of
stuff you are disconnecting to the outside - a gui
element refers to its listeners. For some versions of
Java, there are bugs that will cause gui elements not
to be garbage collected, but this has only happened
with menu elements and JInternalFrames, as far as I
know. 1.5.0 beta had such a bug for menu items, for
example.

To be sure the panel is collected, override finalize()
in the panel class so that it prints a message, like
"panel collected", and call System.gc() after removing
it. That is not guaranteed to work, of course, but in
general it will.

A heap analysis tool makes tracing references simple.
jmp is a fairly good free one. I found that most of them,
even the commercial ones, crash frequently when
applied to our project, but I was able to get jmp to work
to the point I needed it with an average of only two or
three attempts.


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.