Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8844757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:34:02+00:00 2026-06-14T11:34:02+00:00

I am having some problem with CardLayout. I have a panel and a Next

  • 0

I am having some problem with CardLayout. I have a panel and a Next button on it. upon clicking on it i want to display the 2nd panel. In my code, when i click on the Next buton, the next panel is not displayed. Can someone help me solve this ?

package com.test;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.CardLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class CardLay extends JFrame {

    private JPanel contentPane;
    private CardLayout ca;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    CardLay frame = new CardLay();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public CardLay() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        ca =new CardLayout(0, 0);

        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(ca);

        JPanel panel = new JPanel();
        panel.setLayout(null);

        contentPane.add("1",panel);

        JButton btnNext = new JButton("NEXT");
        btnNext.setBounds(131, 93, 117, 29);
        btnNext.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                ca.show(contentPane,"1");
                System.out.println("button clicked");
            }
        });
        panel.add(btnNext);


        JPanel panel_1 = new JPanel();
        contentPane.add(panel_1, "name_1353086933711396000");

        JCheckBox chckbxNewCheckBox = new JCheckBox("New check box");
        panel_1.add(chckbxNewCheckBox);
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:34:03+00:00Added an answer on June 14, 2026 at 11:34 am

    You need to call:

    ca.show(contentPane, "name_1353086933711396000");
    

    For this to work you will have to add the second panel like this:

    contentPane.add("name_1353086933711396000", panel_1);
    

    When using CardLayout make sure to keep navigation buttons on a separate container other then the ‘cards’ themselves, so that they can be visible throughout the navigation process. Here you could place a new navigation container in the frame’s BorderLayout.SOUTH position. For sequential navigation, the methods previous and next are available.

    Also avoid using absolute positioning (null layout). See Doing Without a Layout Manager (Absolute Positioning).

    public CardLay() {
       setDefaultCloseOperation(EXIT_ON_CLOSE);
       setSize(500, 400);
    
       ca = new CardLayout(0, 0);
    
       contentPane = new JPanel();
       contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
       contentPane.setLayout(ca);
    
       JPanel panel1 = new JPanel();
       panel1.add(new JButton("Test Button"));
       contentPane.add("card1", panel1);
    
       JPanel panel2 = new JPanel();
       contentPane.add("card2", panel2);
       JCheckBox chckbxNewCheckBox = new JCheckBox("New check box");
       panel2.add(chckbxNewCheckBox);
    
       JPanel navigationPanel = new JPanel();
    
       JButton btnPrevious = new JButton("< PREVIOUS");
       btnPrevious.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ca.previous(contentPane);
        }
       });
       navigationPanel.add(btnPrevious);
    
       JButton btnNext = new JButton("NEXT >");
       btnNext.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
          ca.next(contentPane);
        }
       });
       navigationPanel.add(btnNext);
    
       add(contentPane);
       add(navigationPanel, BorderLayout.SOUTH);
    }
    

    Recommended: How to Use CardLayout

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code is having some problem with the jQuery. <script type="text/javascript"> $(window).load(function() {
i am having some problem with position: absolute div. IE7 displays it next to
I am having some problem with jsonp and jquery. This is my code -
I'm having some problem consuming REST Service and want to figure out what I'm
I'm building a website and having some problem. if the HTML code is like:
I'm having some problem with this code using the #as_json method. render json: channel.as_json({
I am having some problem in my login form like this I want to
I have been having some problem with the stringByAddingPercentEscapesUsingEncoding: method. Here's what happens: When
i having some problem with TList and BinarySearch. I have this structure: PDoubleEstr =
I'm having some problem with logic of the comparison of some periods. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.