Thursday, June 13, 2024
HomeComputer programmingJava programmingJava: Card Layout Manager (Swing)

Java: Card Layout Manager (Swing)

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.

————————— ————————— —————————
| 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);

// 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();


// 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) {;


public static void main(String args[]) {

CardLayoutManagerExample frame = new CardLayoutManagerExample(“Example”);



*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: [email protected]
-William. § (marvin_gohan)



Please enter your comment!
Please enter your name here

Most Popular


Recent Comments

error: Content is protected !!