TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Java Swing

Java Swing CardLayout

Overview

In this tutorial, we will learn about Java Swing CardLayout.

Swing CardLayout

The CardLayout manages components in a stack where only the top is visible at a given point in time. Users can choose which components to display by making choices through GUI components like buttons, combo boxes, etc.

Java Demo Program

package com.testingdocs.swing.layouts;

/****************************************
 * Filename: CardLayoutDemo.java
 * Package : com.testingdocs.swing.layouts
 * Java Tutorials - www.TestingDocs.com
 *****************************************/

import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.text.AttributeSet.ColorAttribute;
import javax.swing.text.StyleConstants.ColorConstants;


public class CardLayoutDemo {

 public static void main(String[] args) {

 final String card1Text = "Card 1";
 final String card2Text = "Card 2";
 final String card3Text = "Card 3";
 final JPanel cards; 
 // buttons
 final String NEXT = "NEXT";
 final String PREVIOUS = "PREVIOUS";
 
 // Create a frame
 JFrame frame = new JFrame("CardLayout Demo - www.TestingDocs.com");


 //Create cards.
 JPanel card1 = new JPanel();
 card1.add(new JButton("Button Card 1"));

 JPanel card2 = new JPanel();
 card2.add(new JButton("Button Card 2"));

 JPanel card3 = new JPanel();
 card3.add(new JButton("Button Card 3"));

 cards = new JPanel(new CardLayout());
 cards.add(card1, card1Text);
 cards.add(card2, card2Text);
 cards.add(card3, card3Text);


 class ControlActionListenter implements ActionListener {
 public void actionPerformed(ActionEvent e) {
 CardLayout cl = (CardLayout) (cards.getLayout());
 String cmd = e.getActionCommand();
 if (cmd.equals(NEXT)) {
 cl.next(cards);
 } else if (cmd.equals(PREVIOUS)) {
 cl.previous(cards);
 } 
 }
 }
 ControlActionListenter cal = new ControlActionListenter();

 JButton btn1 = new JButton("Next Card");
 btn1.setActionCommand(NEXT);
 btn1.addActionListener(cal);

 JButton btn2 = new JButton("Previous Card");
 btn2.setActionCommand(PREVIOUS);
 btn2.addActionListener(cal);


 // add buttons
 JPanel controlButtons = new JPanel();
 controlButtons.add(btn1);
 controlButtons.add(btn2);
 
 Container pane = frame.getContentPane();
 pane.add(cards, BorderLayout.CENTER);
 pane.add(controlButtons, BorderLayout.PAGE_END);

 //Frame properties
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setSize(400, 200);
 frame.setVisible(true);
 }
}

 

Output

Run the Java application to view the output.

Swing CardLayout

—

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/in/java/

Related Posts

Add GUI Components Swing

Java Swing /

Add GUI Components to a Swing Container

JPanel Container Class

Java Swing /

Swing JPanel Container Class

Java Swing GridLayout

Java Swing /

Java Swing GridLayout

Java Swing FlowLayout

Java Swing /

Java Swing FlowLayout

Java Swing BorderLayout

Java Swing /

Java Swing BorderLayout

‹ Java Swing GridLayout› Swing JPanel Container Class

Recent Posts

  • Update draw.io on Windows
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com