< Prev
Next >

CardLayout Manager

CardLayout manager is used to position the components in a manner of deck of cards, with only the card at the top of deck is visible at a time. All the components of CardLayout Manager are of the type - JPanel and they are contained in a main parent JPanel container. The layout manager of this main parent JPanel is set to CardLayout manager.

Constructors of CardLayout

Constructor Description
public CardLayout() Creates a CardLayout with gap of size zero between the components..
public CardLayout(int hGap, int vGap) Creates a CardLayout with a specified number of gap between the components.

CardLayout methods

Methods Description
public void first(Component comp) This method flips to the first card in the container.
public void next(Component comp) This method flips to the next card in the container.
public void last(Component comp) This method flips to the last card in the container.
public void show(Component comp,String name) This method shows a specific card in the container with a specific name.
public void setHgap(int horizontalGap) This method sets the horizontal space between components.
public void setVgap(int verticalGap) This method sets the vertical space between components.

LayoutManager is an interface which is implemented by CardLayout manager class.

An example display the use of CardLayout Manager.

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

public class CardLayoutEx 
public static void main(String... ar)
SwingUtilities.invokeLater(new Runnable() {
public void run()
new A();

}//Closing the main method
}//Closing the class A

class A implements ActionListener
JFrame jf;
JPanel parentPanel;
CardLayout cd;

jf = new JFrame("CardLayout Example");
cd = new CardLayout();

//Creating a main parent panel that will contain two child panels
parentPanel = new JPanel();

//Creating two child panels.
JPanel childPanel1 = new JPanel();
JPanel childPanel2 = new JPanel();

JButton button1 = new JButton("Alphabets");
JButton button2 = new JButton("Numbers");

JButton button3 = new JButton("4");
JButton button4 = new JButton("5");
JButton button5 = new JButton("6");

//Adding buttons to childPanel1

JButton button6 = new JButton("A");
JButton button7 = new JButton("B");
JButton button8 = new JButton("C");

//Adding buttons to childPanel2

//Setting the positioning of the components in parentPanel, JPanel(that contains childPanel1 and childPanel2) to CardLayout

//Adding childPanel1 and childPanel2 to parentPanel
parentPanel.add(childPanel1, "Num");
parentPanel.add(childPanel2, "Alp");


//Setting container JFrame's layout to FlowLayout.
jf.setLayout(new FlowLayout());

//Adding two buttons to JFrame, these buttons will remain commonly visible to all panels 

//Adding JPanel, parentPanel to JFrame


public void actionPerformed(ActionEvent ae)
//If "Numbers" button is clicked, open the JPanel with buttons showing numbers.

//If "Alphabets" button is clicked, open the JPanel with buttons showing alphabets.

When you run the code, you are presented a window showing the main frame, which contains a parent Panel which further contains two child panels, which looks like Figure1.

Figure 1.

When you click on the button Alphabets you are presented a JPanel with buttons showing you Alphabets, like Figure2.

Figure 2

When you click on the button Numbers you are presented a JPanel with buttons showing you numbers, like Figure3.

Figure 3

Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter

< Prev
Next >
< GridLayout Manager
GridBagLayout Manager >

Please Subscribe

Please subscribe to our social media channels for daily updates.

Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page