wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Loading and unloading a panel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Loading and unloading a panel" Watch "Loading and unloading a panel" New topic
Author

Loading and unloading a panel

Darrell Morello
Ranch Hand

Joined: Apr 28, 2005
Posts: 38
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
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

CardLayout is what you're probably looking for.

You add a main panel to the split pane (lets call this cardPanel), and give it an instance of CardLayout (call it cardLayout).
You add every panel (or other component) you need to show in the card panel with a unique identifier, and you show one using cardLayout.show(cardPanel, identifier). The only thing the buttons need to know is the identifier of the panel, plus references to the card panel and layout.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Darrell Morello
Ranch Hand

Joined: Apr 28, 2005
Posts: 38
So this means I dont have to create and destroy panels? Will it take up much memory? And can I control the cardlayout panel even though I have the buttons on another panel?

Thanks
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Darrell Morello:
So this means I dont have to create and destroy panels? Will it take up much memory? And can I control the cardlayout panel even though I have the buttons on another panel?


Yes, no, yes.

Moving to Swing/AWT...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

And now with some explanations
Originally posted by Darrell Morello:
So this means I dont have to create and destroy panels?

You need to create each panel once, then you can use that one instance.
Will it take up much memory?

Well, it has one instance of all panels active all the time. So it is more memory consuming than having only the instance you display in memory. However, since you know nothing about garbage collection except that you know nothing, you might still have all panels in memory even though you have only one reference. So basically, the amount of memory this solution needs may be higher than having only one reference, but it might not. Furthermore, if you only create panels once and then hide them, there is no difference in memory.
And can I control the cardlayout panel even though I have the buttons on another panel?

As long as those buttons / their action(listener)s have a reference to both the panel and the layout.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loading and unloading a panel
 
Similar Threads
Switching between panels in a GUI
[RichFaces] a4j:commandButton, panel, modalpanel and hotkeys
regarding images as buttons in the form
About design pattern used in SCJD
JViewPort problems