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 / June 2007

Tip: Looking for answers? Try searching our database.

Reusing JFrame: clearing owned windows?

Thread view: 
Karsten Wutzke - 15 Jun 2007 11:46 GMT
Hello all!

In my app, I want to reuse the main app frame. Anytime I switch
language or do some other major application "model" update, I recreate
the whole GUI. Since I want to avoid a flickering frame, I want to
reuse just that. So in one of my high level classes, I keep an
instance reference to that frame. Anytime buildGui() gets called, I
remove everything from the content pane and readd the new components.
So far so good...

Take the following code (untested and just as visualization):

public class Controller
{
   private final JFrame frm = new JFrame("Main");
   private JDialog dlgUserData;

   private final UserData ud = UserData.getInstance();

   public void buildGui()
   {
       //try to remove dialog from frame owned windows *if it
exists*...
       //(has absolutely no effect, just a desparate try)
       if ( dlgUserData != null )
       {
           //hmmm.... just trying...
           dlgUserData.removeAll();

           //mutual remove doesn't work
           dlgUserData.remove(frm);
           frm.remove(dlgUserData);
       }

       //build new components first....

       //"userdata" (dialog title) now usually is a string in a
different language.....
       dlgUserData = new JUserDataDialog(frm, "userdata<lcl>",
ud);         //<- passing frm

       ... panels, menu bar etc.

       //all new components are ready to be added...

       //frm.removeAll(); //really destroys the frame internals
completely
       frm.getContentPane().removeAll(); //clear GUI

       frm.setJMenuBar(...);
       frm.getContentPane().add(new JUserDataPanel(ud));

       frm.setVisible(true);
   }

}

The problem is when I instantiate several (modal) JDialogs which I
pass that reused JFrame every time the GUI is rebuilt. The frame
stacks up n just created dialog references on every GUI rebuild,
effectively owning all those dialogs (call JFrame.getOwnedWindows() to
see them). So in effect I unwantedly *add* n dialog references owned
by the frame... imagine a running app rebuilding the GUI 30 times...
in my case, the frame would own more than 100 references, most of them
which should be made available for garbage collection! When using 10
dialogs like this this would mean nearly 300 dialog references (of
which I don't know if they will *ever* be GC'd....)

How do I get rid of all those owned windows?? I see
JFrame.getOwnedWindows, but where is its counterpart?

I simply want to avoid a flickering main frame on GUI rebuild...

Does anyone have a working solution for this? If so, how does it work?

I might be doing something wrong here conceptually, so I'm all ears
for a better high level setup...

TIA
Karsten
Knute Johnson - 17 Jun 2007 18:33 GMT
> Hello all!
>
[quoted text clipped - 76 lines]
> TIA
> Karsten

When you are done with a JDialog you should dispose() it.  Then if you
remove all references it will eventually get garbage collected.  I found
that out the hard way with OutOfMemoryExceptions in a computer that had
a gig of RAM.  It took a couple of weeks but it would blow up every time.

Signature

Knute Johnson
email s/nospam/knute/



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.