
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Importance of the CardLayout Class in Java
In this article, we will learn about the importance of the CardLayout class in Java. In Swing, we usually have to manage a set of views and panels in a single container. The CardLayout class provides you with a flexible and powerful method to complete this, allowing you to switch between various components, such as cards in a deck.
What is CardLayout?
CardLayout is a layout manager in the Java AWT package. It is different from the other layouts, where the other layout managers attempt to display all the components within the container at once, the CardLayout displays only one component at a time. In the CardLayout, cards are usually placed in a container such as a JPanel. The components are placed into the card queue in the order in which they are added.
Initialization of a CardLayout:
CardLayout cardLayout = new CardLayout();
Methods in CardLayout
The important methods of CardLayout are:
- first()
- last()
- next()
- previous()
- show()
Why is CardLayout Important?
The following are the key features of CardLayout in Java:
Space Efficiency in GUIs:
- CardLayout enables developers to create complex interfaces without requiring large screen real estate.
- Different views or panels can be provided within the same area of a container, thus making the interface cleaner.
Dynamic UI Creation:
- With CardLayout, we can create UI dynamically based on user and application interaction.
- This is very useful in cases such as wizards, tabbed panels, and finally all those applications that need to show different panels conditionally.
Tabbed Interface Alternative:
- For such types of interfaces, Java provides the JTabbedPane class.
- But it is also backed by the extra programmatic control, which allows the customization on how and when a user interface switches between panels.
Memory Optimization:
- Since only the currently visible component is rendered, CardLayout can help reduce memory usage compared to solutions where all panels are always visible (but perhaps hidden).
Example of CardLayout
Below is an example of CardLayout on Swing GUI in Java:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CardLayoutTest extends JFrame implements ActionListener { CardLayout card; JButton b1,b2,b3; Container con; CardLayoutTest() { con = this.getContentPane(); card = new CardLayout(40,30); con.setLayout(card); b1 = new JButton("Java"); b2 = new JButton("Python"); b3 = new JButton("Scala"); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); con.add("a", b1); con.add("b", b2); con.add("c", b3); } public void actionPerformed(ActionEvent e) { card.next(con); } public static void main(String[] args) { CardLayoutTest clt = new CardLayoutTest(); clt.setTitle("CardLayout Test"); clt.setSize(350, 275); clt.setLocationRelativeTo(null); clt.setDefaultCloseOperation(EXIT_ON_CLOSE); clt.setVisible(true); } }
Output
Practical Examples of CardLayout Usage
The following are the practical use cases for CardLayout in Java:
-
Login/Application Switching: First, you see the login page. After that, you can switch to the main application panel once you log in.
-
Configuration Wizards: In a configuration wizard, each step (like Step 1: User Info, Step 2: Preferences, Step 3: Summary) you see this one at a time, and by using CardLayout, you can see that each step is placed on a separate panel.
-
Game Interfaces: By using CardLayout, it is easy to switch between menu screens, game boards, and score displays.
- Dashboard Apps: The dashboard applications keep multiple visual data images in one place. Here, we can switch between different dashboard views easily using buttons or menu options, without opening multiple windows.