Reusing JFrame: clearing
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
//(has absolutely no effect, just a desparate try)
if ( dlgUserData != null )
//hmmm.... just trying...
//mutual remove doesn't work
//build new components first....
//"userdata" (dialog title) now usually is a string in a
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
frm.getContentPane().removeAll(); //clear GUI
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...
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24