Loading and unloading a panel

Hi all

I have a Main JPanel with a JSplitPane and on the two parts of the split pane are two more panels, panel A and panel B. The panel on one side (eg. A )contain some buttons. Each button calls a panel or form that must be shown on panel B. When another button is pressed, the form should disappear and the panel to which the second button is for, should be displayed.

Do I have to create an instance of panel B in panel A so that the buttons can control whats in panel B? Also, is there a better way than using setVisible, like a way which I can create the instance when needed and remove it when another panel is being loaded or created?

Thanks

[674 byte] By [morelloa] at [2007-10-2 3:53:01]
# 1
Just to add some more info, I need to load and unload (or change or create/destroy) the panels using the most memory efficient way. I also need to control this using buttons on another panel.
morelloa at 2007-7-15 23:09:24 > top of Java-index,Desktop,Core GUI APIs...
# 2
I would use a [url http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html]Card Layout[/url].
camickra at 2007-7-15 23:09:24 > top of Java-index,Desktop,Core GUI APIs...
# 3
Is it possible to control whats displayed even though the buttons are on another panel? It would help if you can give me an example or a reference :)How about the memory? Will everything on the cardlayout panel be loaded on program start? If so, wouldnt that make it
morelloa at 2007-7-15 23:09:24 > top of Java-index,Desktop,Core GUI APIs...
# 4
> It would help if you can give me an example or a reference :)I did. Did you read the tutorial?
camickra at 2007-7-15 23:09:24 > top of Java-index,Desktop,Core GUI APIs...
# 5
Thanks for the link camickr. Yes, I read it but how about, if I want to control the cards from a button on another panel on the other side of the JSplitPane?
morelloa at 2007-7-15 23:09:24 > top of Java-index,Desktop,Core GUI APIs...
# 6
And how is that different then controlling it from a combo box which is on a different panel? You are simply responding to an event. It doesn't matter where the event is generated from.
camickra at 2007-7-15 23:09:24 > top of Java-index,Desktop,Core GUI APIs...
# 7

Easiest way to do this is to simply invoke splitPane.setRightComponent(myPanel);

where myPanel is the panel that should be shown for the button that was clicked. If you created a new panel each time the button was clicked rather than holding a reference to it the old panel should get collected. You'll have to be careful about not allowing any references to persist.

kablaira at 2007-7-15 23:09:24 > top of Java-index,Desktop,Core GUI APIs...
# 8

And for the record I agree with carnickr that CardLayout will probably achieve better results. However, if each panel is not likely to be reused, or there are thousands of panels, or each panel requires an abnormally large amount of memory, the alternative of changing the component to a new instance might suit you.

kablaira at 2007-7-15 23:09:24 > top of Java-index,Desktop,Core GUI APIs...