About CardLayout

CardLayout places components (usually panels) on top of each other in a stack like a deck of cards. You see only one at a time, and you can flip through the panels by using another control to select which panel comes to the top.

CardLayout is a good layout to use when you have an area that can contain different components at different times. This gives you a way to manage two or more panels that need to share the same display space.

An example of using CardLayout can be seen by looking at the wizards in JDeveloper. The Cancel, Next, and Back buttons control which panel is displayed next.

To create a CardLayout container:

  1. Create a new Application.
  2. Right-click the frame file in the Navigator and choose Open.
  3. Add a panel to your UI in the Java Visual Editor and set its layout property to CardLayout.
  4. Drop a new panel into the CardLayout panel. This new panel will completely fill up the CardLayout panel.

    Note:The first component you add to a CardLayout panel will always fill the panel.

  5. Set the layout property for this new panel to XYLayout and add the desired components.
  6. Click a panel on the Component Palette, then click on the CardLayout panel in the component tree in the Structure window to add it to the stack in CardLayout panel.
  7. Set this second panel to XYlayout and add components to it.
  8. Repeat steps 6 and 7 for each new panel you want to add to the stack.

Specifying the Gap Surrounding a CardLayout Container

Using the Structure window, you can specify the amount of horizontal and vertical gap surrounding a stack of components in a CardLayout .

To specify the horizontal and vertical gap in a CardLayout:

  1. Select the cardLayout object in the Structure window, displayed immediately below the container it controls.

    Structure window image

  2. Click the hgap (horizontal gap) or vgap (vertical gap) property in the Inspector.
  3. Enter the number of pixels you want for the gap.
  4. Press Enter or click anywhere else in the Inspector to register the changes.