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]

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
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?
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.
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.