12.4. The CardLayout Manager

The CardLayout manager stacks components on top of each other, displaying one at a time. To use this layout manager, you create a CardLayout instance, telling the window to use the manager but also saving the reference in a variable:

Panel cardPanel;
CardLayout layout;
...
layout = new CardLayout();
cardPanel.setLayout(layout);

Don't forget to save a reference to the layout manager; you'll need the reference later. You then associate a name with each component you add to the window, as below:

cardPanel.add(component1, "Card 1");
cardPanel.add(component2, "Card 2");
...

The first component added is shown by default. You tell the layout manager to display a particular component in a number of ways: for example, ...

Get Core Web Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.