CardLayout

The CardLayout layout manager is significantly different from the other layouts. Whereas the other layout managers attempt to display all the components within the container at once, a CardLayout displays only one component at a time. (That component could be a Component or another Container.) The result is similar to Netscape Navigator's Property sheets or a tabbed Dialog, without the tabs. You can flip through the cards (components) in the layout in order or jump to a specific card if you know its name. The following call to setLayout() changes the LayoutManager of the current container to CardLayout:

lm = new CardLayout(); setLayout (lm); 

Unlike most other layout managers, CardLayout has a number of instance methods that programs have to call. Therefore, you usually have to retain a reference to the layout manager. In addition, you usually have some other component to control the CardLayout (i.e., select which card to view). Most simply, you could put some buttons in a panel and stick this panel in the north region of a BorderLayout; then make another panel with a CardLayout, and place that in the center. A more complex task would be to build a set of tabs to control the CardLayout.

A CardLayout allows you to assign names to the components it manages. You can use the name to jump to an arbitrary component by calling the manager's show() method. In Java 1.0, naming was optional; you could call add(Component) to put a component in the layout with a null name. A null name meant only that you couldn't flip to the component at will; you could only display the component by calling next() or previous() (or first() or last()), which cycle through all the components in order. In Java 1.1, all components added to a CardLayout must be named.

CardLayout Methods

Constructors Informational methods LayoutManager methods LayoutManager2 methods CardLayout methods

This group of methods controls which component the CardLayout displays. The show() is only usable if you assigned components names when adding them to the container. The others can be used even if the components are unnamed; they cycle through the components in the order in which they were added. All of these methods require the parent Container (i.e., the container being managed by this layout manager) as an argument. If the layout manager of the parent parameter is anything other than the container using this instance of the CardLayout, the method throws the run-time exception IllegalArgumentException.

Miscellaneous methods
java.awt.CardLayout[hgap=0,vgap=0] 

CardLayout Example

Figure 7.7 shows a simple CardLayout. This layout has three cards that cycle when you make a selection. The first card (A) contains some Checkbox items within a Panel, the second card (B) contains a single Button, and the third (C) contains a List and a Choice within another Panel.

Figure 7.7: Different views of CardLayout

[Graphic: Figure 7-7]

Example 7.1 is the code that generated Figure 7.7.

Example 7.1: The CardExample Class

import java.awt.*; import java.applet.*;
public class CardExample extends Applet {
 CardLayout cl = new CardLayout();
public void init () {
 String fonts[] = Toolkit.getDefaultToolkit().getFontList(); setLayout (cl); Panel pA = new Panel(); Panel pC = new Panel (); p1.setLayout (new GridLayout (3, 2)); List l = new List(4, false); Choice c = new Choice ();
for (int i=0;i<fonts.length;i++) {
 pA.add (new Checkbox (fonts[i])); l.addItem (fonts[i]); c.addItem (fonts[i]);
}
pC.add (l); pC.add (c); add ("One", pA); add ("Two", new Button ("Click Here")); add ("Three", pC);
}
public boolean action (Event e, Object o) {
 cl.next(this);
return true;
}
} 

Each panel within the CardLayout has its own layout manager. Panel A uses a GridLayout; panel C uses its default layout manager, which is a FlowLayout. When the user takes any action (i.e., clicking on a checkbox or button, or selecting an item from the List or Choice components), the system generates a call to action(), which calls the CardLayout's next() method, thus displaying the next card in the sequence.