CardLayout as Layout Manager in Java

May 28, 2020
Tutorials
cardlayout

This instructional exercise is about the CardLayout as Layout Manager in Java. A CardLayout is a layout manager is utilized like a container of at least two components where it is represented as a card. Hence, there is just one card that is visible at a time, and the container acts as the storage of cards.

So, now let’s start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of cardLayout.java.

2. Import following package library:

import java.awt.*; //use to access the CardLayout class
import java.awt.event.*; //used to access the ActionEvent and ActionListener class
import javax.swing.*; //used to access JWindow, JPanel, and JButton class

3. We will extend a JPanel and implement an ActionListener of the button into the class name. Initialize your variable in your Main, variable card for the CardLayout. Have this code below:

CardLayout card = new CardLayout(40, 40); // Create layout

As you can see above, the two parameters inside the CardLayout were the width and the height of the card layout.  The width and height is 40.

4. Now, we will make a constructor. We will set the layout into cardLayout using the setLayout method and will initialize a button.

setLayout(card);
   JButton button;

 

Inside the constructor, we will create a looping and an ActionListener to access the other cards inside the button.

  for (int i = 1; i <= 10; i++) {
    add(button = new JButton(" Card " + i), "Card"); // Add a button
    button.addActionListener(this); // Add listener for button
  }
}

 

5. Now, we will handle the button events. There are five methods that are commonly used for the card layout. Namely, the nextpreviouslast, first, and show method.

public void actionPerformed(ActionEvent e) {
  card.next(this); // Switch to the next card
  card.previous(this); // Switch to the previous card
  card.last(this); // Switch to the last card
  card.first(this); // Switch to the first card
}

 

6. Now, code for your Main, initialize variable window for JWindow. Lastly, set the visibility and size, and add the CardLayout as the layout of the window. Have this code below:

public static void main(String[] args) {
  JWindow window= new JWindow();
  window.setSize(400,400);
  window.getContentPane().add(new cardLayout());
  window.setVisible(true);
}

 


Output:

output

output

output


Here’s the full code of this tutorial:

import java.awt.*; //use to access the CardLayout class
import java.awt.event.*; //used to access the ActionEvent and ActionListener class
import javax.swing.*; //used to access JWindow, JPanel, and JButton class
 
public class cardLayout extends JPanel implements ActionListener {
  CardLayout card = new CardLayout(40, 40); // Create layout
 
  public cardLayout() {
    setLayout(card);
    JButton button;
 
    for (int i = 1; i <= 10; i++) {
      add(button = new JButton(" Card " + i), "Card"); // Add a button
      button.addActionListener(this); // Add listener for button
    }
  }
  // Handle button events
  public void actionPerformed(ActionEvent e) {
    card.previous(this); // Switch to the previous card
  }
 
  public static void main(String[] args) {
    JWindow window= new JWindow();
    window.setSize(400,400);
    window.getContentPane().add(new cardLayout());
    window.setVisible(true);
  }
}

 

 

Facebook Comments

Leave a Reply

Your email address will not be published.