Table of contents
1.
Introduction
2.
Constructors of CardLayout in Java
3.
Methods of CardLayout class in Java
4.
Example for CardLayout in Java
4.1.
Example 1 - Using Default Constructor
4.2.
Java
4.3.
Example 2 - Using Parameterized Constructor
5.
Frequently Asked Questions 
5.1.
Which layout arrange the components as a deck of cards such that only one component is visible at a time?
5.2.
What are common mistakes when using CardLayout?
5.3.
Can CardLayout manage multiple components simultaneously?
5.4.
What are the alternatives to CardLayout in Java?
5.5.
What are the limitations of CardLayout?
6.
Conclusion
Last Updated: Sep 25, 2025
Medium

CardLayout in Java – Switch Panels & Layout Control

Author vishal teotia
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Java, creating dynamic and interactive user interfaces is made easier with various layout managers, one of the most useful being CardLayout. This layout manager is ideal for situations where you need to display different panels (or "cards") in the same space, switching between them based on user interaction. Think of it as a deck of cards, where only one card is visible at a time, and the user can flip through them.

card layout in java

Card Layout shows only the topmost card at a time, similar to a deck of playing cards. All components in a container are treated as Cards, with the container itself acting as a Stack. Cards in a container are ordered internally. It is the very first component present in the container to be visible when it is displayed for the first time.

Constructors of CardLayout in Java

The CardLayout class in Java has a number of constructors. The following are some CardLayout constructors in Java:

  • CardLayout(): With the help of this constructor, a new CardLayout can be created with gaps between its components of size zero (0).  
  • CardLayout(int hgap, int vgap): As stated in the arguments, this constructor of Java is used to create a new CardLayout with vertical and horizontal gaps between the components. Vgap represents the vertical gap between components, while Hgap refers to the horizontal gap between components.  

Methods of CardLayout class in Java

The methods of CardLayout class are listed below:

Method nameMethod description
public void first (Container parent)In this method, the first card from the container is flipped
public void last (Container parent)The method for flipping the last card in the container
public void next (Container parent)Flipping the container to the next card is the method used
public void previous (Container parent)Flipping the container to the previous card is the method used
public void show(Container parent, String name)Flipping to the container with the given name with the specified method
getVgap()A method for determining vertical gaps between components
getHgap()Obtaining the horizontal gap between components using this method
void addLayoutComponent(Component com, Object constraints)Method of adding a component to the internal component table of a card layout
float getLayoutAlignmentX(Container parent)This method returns an x-axis alignment
float getLayoutAlignmentY(Container parent)This method returns an y-axis alignment
Dimension maximumLayoutSize(Container target)Given the target container and the component in it, this method returns the layout's maximum dimensions.
Dimension mimimumLayoutSize(Container target)Based on the container in which the component resides, the method returns a minimum dimension for the layout.
Dimension preferredLayoutSize(Container parent)The container argument size is determined using the card layout in this method
void removeLayoutComponent(Component comp)As a result of this method, the specified component is removed from the card layout.
String toString()A string representation of the layout of this card is returned by this method
void invalidateLayout(Container target)As a result of this method, the layout manager discards any cached information that may have been retained.

 

Example for CardLayout in Java

Java examples of CardLayout are given below:

Example 1 - Using Default Constructor

  • Java

Java

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.
JFrame;
interface of ActionListener
public class Cardlayout extends JFrame implements ActionListener {
CardLayout cd;
JButton jb1, jb2, jb3;
Container con;
above objects
Cardlayout()
{
con = getContentPane();
cd = new CardLayout(70, 50);
con.setLayout(cd);
jb1 = new JButton("Hello");
jb2 = new JButton("Hey");
jb3 = new JButton("Hii");
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
con.add("a", jb1);
con.add("b", jb2);
con.add("c", jb3);
}
public void actionPerformed(ActionEvent e)
{
cd.next(con);
}


public static void main(String[] args)
{
Cardlayout cl1 = new Cardlayout();
cl1. setTitle("Checking how Card Layout works");
cl1.setSize(800, 800);
cl1.setResizable(false);
cl1.setVisible(true);


cl1.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
You can also try this code with Online Java Compiler
Run Code


Output:

In the example below, the output would be a JFrame with the first button with the name "Hello" displayed first:

Output of example

The second button, "Hey", appears when you click it:

Output of example

As soon as the user clicks its button, "Hii" will appear.

Output of example

Example 2 - Using Parameterized Constructor

In this example, we will use a parameterized constructor to initialize the CardLayout and add cards with the same functionality as the previous example.

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class CardLayoutExample extends JFrame implements ActionListener {
    CardLayout cd;
    JButton jb1, jb2, jb3;
    Container con;

    // Parameterized constructor
    CardLayoutExample(int hGap, int vGap) {
        con = getContentPane();
        cd = new CardLayout(hGap, vGap);  // Passing horizontal and vertical gap to the CardLayout constructor
        con.setLayout(cd);

        // Creating the buttons
        jb1 = new JButton("Hello");
        jb2 = new JButton("Hey");
        jb3 = new JButton("Hii");

        // Adding ActionListeners
        jb1.addActionListener(this);
        jb2.addActionListener(this);
        jb3.addActionListener(this);

        // Adding the buttons to the container with unique names
        con.add("a", jb1);
        con.add("b", jb2);
        con.add("c", jb3);
    }

    // Action event handler
    public void actionPerformed(ActionEvent e) {
        cd.next(con);  // Switch to the next card on button click
    }

    public static void main(String[] args) {
        // Passing horizontal and vertical gap values to the constructor
        CardLayoutExample cl1 = new CardLayoutExample(70, 50);
        cl1.setTitle("Using Parameterized Constructor for Card Layout");
        cl1.setSize(800, 800);
        cl1.setResizable(false);
        cl1.setVisible(true);

        cl1.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

Frequently Asked Questions 

Which layout arrange the components as a deck of cards such that only one component is visible at a time?

The `CardLayout` in Java arranges components like a deck of cards, displaying only one component at a time. It is useful for creating wizard dialogs or tabbed panels where you need to switch between different components.

What are common mistakes when using CardLayout?

Common mistakes include not setting the correct layout, not managing card visibility properly, or failing to update components after switching cards, leading to UI inconsistencies. Another mistake is adding components to the container without specifying unique card names.

Can CardLayout manage multiple components simultaneously?

No, CardLayout only displays one component (card) at a time. It’s designed to switch between different views or panels, but it does not support displaying multiple components simultaneously.

What are the alternatives to CardLayout in Java?

Alternatives to CardLayout include JTabbedPane for tabbed navigation, Panel with custom logic, and JPanel with different layout managers like GridLayout or BorderLayout for more complex layouts.

What are the limitations of CardLayout?

CardLayout is limited to displaying one card at a time, making it unsuitable for complex multi-component layouts. It also lacks customization options for transitioning between cards and does not handle overlapping components effectively.

Conclusion

Layouts in Java come in a variety of types, and each type has its own arrangement of components. Programmers must have a thorough understanding of every layout in order to work efficiently with GUI applications. In the IT sector, Layout Managers are also popular in Graphics Programming.

Live masterclass