This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Switching between panels in a GUI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Switching between panels in a GUI" Watch "Switching between panels in a GUI" New topic

Switching between panels in a GUI

Peter Desjardins

Joined: Jun 13, 2011
Posts: 9
Hi. I'm very new to developing GUIs and I need to switch between two sets of controls. I have a set of radio buttons to switch between two modes. Each mode has its own set of controls that should appear when the corresponding radio button is selected.

To do this, I added a Panel to my GUI and then added another Panel containing the controls for one mode (the subcontrols) to the first panel. Whenever the mode switches, I plan to remove the current child panel and replace it with a new panel containing the correct subcontrols for the new mode.

I'm using these parent and child Panels so that I can choose where the subcontrols go in the GUI. I place the parent Panel in my GUI and then I can call add() and remove() on it as needed. The subcontrols always stay in the same screen location.

What I find is that the subcontrols are visible when the GUI is first rendered. However, when I switch the radio buttons, the first set of subcontrols are removed and the new set of subcontrols are not visible. I know that the parent Panel remains the same size but the child Panel has a size of 0,0.

Here are the methods I'm using to switch between the control sets. When the GUI is initially created, I call initializeSpecializedControls(). Then when one or the other radio button is selected, it calls either showUnitsControls() or showBuildingControls(). I know the last two methods are being called when I select the radio buttons. I'm surprised that calling them doesn't create a full-sized and visible set of controls because the only difference between them and the initialize method is that the show*Controls() methods also remove the existing Panel.

Is there a simpler way to switch some of the controls in a GUI depending on a mode selection? I've tried setting the size of the child Panel explicitly and I can make it appear on the screen. However, it's controls are not present, only the background color. The UnitControls() and BuildingControl() contructors create the controls the first time, why would they behave differently later on?

I'm using JDK 1.6. I apologize for some vagueness here. Any thoughts will be appreciated.

Thanks for your help.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2454
Sounds like you should use a CardLayout.
Peter Desjardins

Joined: Jun 13, 2011
Posts: 9
That's just what I needed. I was trying to roll my own, badly.


I agree. Here's the link:
subject: Switching between panels in a GUI
It's not a secret anymore!