File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Switching between panels in a GUI" Watch "Switching between panels in a GUI" New topic
Author

Switching between panels in a GUI

Peter Desjardins
Greenhorn

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: 2191
    
    7
Sounds like you should use a CardLayout.
Peter Desjardins
Greenhorn

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

Thanks!

Peter
 
GeeCON Prague 2014
 
subject: Switching between panels in a GUI