Introduction to CardLayout in Java
The following article CardLayout in Java, provides an outline for the different methods of CardLayouts in java as we are aware of the concept of Layout in Java and how the different Layout Manager helps in managing so many components in a single container without affecting the positioning of each other. The card layout is one of them. Unlike other layouts, which display the components of a container one at a time, Card Layout, as the name indicates, works like a deck of playing cards with only one card, i.e., the topmost card visible at a single time. It treats every component in a container as a Card, and the container acts as a Stack of cards. The ordering of the cards in a container is defined internally. When the container is displayed for the first time, it is the first component present in the container that is visible at that time.
Constructors of CardLayout in Java
CardLayout is a class of java, and it has some constructors. Below given are some of the Constructors of CardLayout in Java:
CardLayout()
This constructor of Java class CardLayout is used to create a new CardLayout with the gaps of size zero (0) between the different components.
CardLayout(int hgap, int vgap)
As mentioned in the arguments, this java constructor is used to create a new CardLayout with the horizontal and vertical gap between the components. Hgap denotes the horizontal gap, whereas vgap represents the vertical gap between the components.
Methods of CardLayout class in java
Below given is the list of the methods of CardLayout class:
Method name | Method description |
public void first (Container parent) | The method used to flip to the first card of the container provided |
public void last (Container parent) | The method used to flip to the last card of the container provided |
public void next (Container parent) | The method used to flip to the next card of the container provided |
public void previous (Container parent) | The method used to flip to the previous card of the container provided |
public void show(Container parent, String name) | The method used to flip to the specified container with the given name |
getVgap() | The method used to get the vertical gap between the components |
getHgap() | The method used to get the horizontal gap between the components |
void addLayoutComponent(Component com, Object constraints) | The method used to add the specified component to the card layout internal table of components |
float getLayoutAlignmentX(Container parent) | The method returns the alignment along the x-axis |
float getLayoutAlignmentX(Container parent) | The method returns the alignment along the y-axis |
Dimension maximumLayoutSize(Container target) | The method returns the maximum dimensions for the layout given the component in the target container. |
Dimension mimimumLayoutSize(Container target) | The method returns the minimum dimensions for the layout given the component in the target container. |
Dimension preferredLayoutSize(Container parent) | In this method, using the card layout, the preferred size of the container argument is determined |
void removeLayoutComponent(Component comp) | This method removes the component specified from the card layout |
String toString() | This method returns the string representation of this card layout |
void invalidateLayout(Container target) | This method invalidates the container, indicating if the layout manager has cached some information; it should be discarded. |
Example for CardLayout in Java
Following is an example of implementing cardlayout in java:
Code:
// importing all the necessary packages
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.*;
// Class Cardlayout is extending the JFrame and implementing the interface of ActionListener
public class Cardlayout extends JFrame implements ActionListener {
// Declaring the objects of the above mentioned Cardlayout class.
CardLayout cd;
// Declaring the objects of JButton class which we want in our JFrame
JButton jb1, jb2, jb3;
// Declaring the object of the Container class with the name ‘con’.
Container con;
// Using the constructor of the class CardLayout in order to initialise the above objects
Cardlayout()
{
// using the method in order to get the content
con = getContentPane();
// Initializing the object "cd” of CardLayout class with horizontal and vertical spaces as 70 and 50 respectively
cd = new CardLayout(70, 50);
// setting of the layout using the setLayout method
con.setLayout(cd);
// Initialising the object "jb1" of the above JButton class.
jb1 = new JButton("Hello");
// Initialising the object "jb2" of the above JButton class.
jb2 = new JButton("Hey");
// Initialising the object "jb3" of the above JButton class.
jb3 = new JButton("Hii");
// Using this Keyword in order to refers to the current object.
// Adding of Jbutton "jb1" on JFrame using the methods of ActionListener
jb1.addActionListener(this);
// Adding of Jbutton "jb2" on JFrame.
jb2.addActionListener(this);
// Adding of Jbutton "jb3" on JFrame.
jb3.addActionListener(this);
// Adding of the above buttons to the container one by one
// Adding the JButton "jb1" using add method
con.add("a", jb1);
// Adding the JButton "jb2" similar to the above
con.add("b", jb2);
// Adding the JButton "jb3" in the container
con.add("c", jb3);
}
public void actionPerformed(ActionEvent e)
{
// using next method to call the next card
cd.next(con);
}
// Main Method of Java class from where the execution starts
public static void main(String[] args)
{
// Creating Object of CardLayout class.
Cardlayout cl1 = new Cardlayout();
// Setting the title of JFrame
cl1. setTitle("Checking how Card Layout works");
// Setting the size of JFrame.
cl1.setSize(800, 800);
// Setting the resizable value of JFrame.
cl1.setResizable(false);
// Setting the visibility of JFrame.
cl1.setVisible(true);
// Function to set default operation of JFrame.
cl1.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Output:
So as given below, the output would be a JFrame with the first button with the name “Hello” displayed first:
Clicking on it, the second button, “Hey”, is displayed:
And then, clicking on its button, “Hii” is displayed to the user.
Explanation:
In the above example, Cardlayout is the class name inheriting the JFrame and implementing the ActionListener interface. We are trying to arrange the various JLabel components in a JFrame. We are creating 3 jButtons with the names jb1, jb2, jb3 and adding them to the JFrame. You can use the add() method to add buttons to the JFrame. In the main function, various methods are used, like setVisible() to set the frame’s visibility, setResizable to set the resizability, setTitle, and setSize for setting the title and size of the frame.
Conclusion
There are various types of layouts in java, and every layout has its way of arranging the components. A deep and practical understanding of every layout is fundamental for a programmer to work efficiently on GUI applications. Graphics Programming also uses Layout Managers in its development, which is trending in the IT industry.
Recommended Articles
This is a guide to CardLayout in Java. Here we discuss the basic concept, constructors, and methods of Cardlayout in Java, along with some examples. You may also look at the following articles to learn more –