Java: Card Layout Manager (Swing)

Posted February 20, 2006 by William_Wilson in Java programming

The following tutorial describes how to use the Card Layout Manager.


Layout Managers are used in the organization of Panels and Frames. The proper layout should be chosen to accommodate frame resizings and use.

The + signs represent empty space.

Card:
————————— ————————— —————————
| Frame Title |▄|▐ |X| | Frame Title |▄|▐ |X| | Frame Title |▄|▐ |X|
————————— ————————— —————————
|++++++++++++++| |++++++++++++++| |++++++++++++++|
|++++++++++++++| |++++++++++++++| |++++++++++++++|
|++++ Card 1 ++++ | | ++++ Card 2 ++++| |++++ Card 3 ++++ |
|++++++++++++++| |++++++++++++++| |++++++++++++++|
|++++++++++++++| |++++++++++++++| |++++++++++++++|
—————————- ————————— —————————-
*where all three Cards are the same frame, displayed at different times

There are two available constructors for this manager, just like the border layout:

public CardLayout()
-the default, gaps of 0 will likely be supplied
public CardLayout(int hgap, int vgap)
-where you may supply the horizontal and vertical gap as desired

This Layout Manager also involves stacking the frames in the porper order. When calling the .next function, the frame which was added in sequence after the currently displayed frame is the next frame.

public void first(Container owner)
-the first frame to be shown
public void next(Container owner)
-display the next frame
public void previous(Container owner)
-display the previous frame
public void last(Container owner)
-display the last frame
public void show(Container owner, String name)
-displays [n]name as text.

CODE Example:
import java.awt.*;
import java.awt.event.*; // need this to handle button presses
import javax.swing.*;
public class CardLayoutManagerExample extends JFrame {

private CardLayout cardLayoutManager;
public CardLayoutManagerExample(String title) {

super(title); //must be first line of this method
Container contentPane = getContentPane();
JButton firstButton, secondButton, thirdButton;
ActionListener listener;

firstButton = new JButton(“one”);
secondButton = new JButton(“two”);
thirdButton = new JButton(“three”);

// Set the layoutManager
cardLayoutManager = new CardLayout(2,2);
contentPane.setLayout(cardLayoutManager);

// Add (and give names to) components using the layout manager
contentPane.add(“first”, firstButton);
contentPane.add(“second”, secondButton);
contentPane.add(“third”, thirdButton);

// set up listener to check for button clicks in order
// to go to the next button in the stack
listener = new ButtonListener();
firstButton.addActionListener(listener);
secondButton.addActionListener(listener);
thirdButton.addActionListener(listener);

}

// private internal class that implements the listener behavior,
// e.g. go to next button in the stack
class ButtonListener implements ActionListener {

public void actionPerformed(ActionEvent theEvent) {
cardLayoutManager.next(getContentPane());
}

}

public static void main(String args[]) {

CardLayoutManagerExample frame = new CardLayoutManagerExample(“Example”);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(100,100);
frame.setVisible(true);

}

}

*NOTE: This example uses a button click to change slides, but try removing the action listeners. Do you know what will happen?

*NOTE: This frame’s buttons do not actually do anything. This is simply an example of using the Layout Manager.
-Try changing the fram.setResizable to true, and resize the frame.

Questions or Comments: william_a_wilson@hotmail.com
-William. ยง (marvin_gohan)

The Conversation

Follow the reactions below and share your own thoughts.