Sharon Watinsan
Sharon Watinsan

Reputation: 9850

CardLayout display Next panel - java Swing

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);
    }

}

Upvotes: 2

Views: 2754

Answers (1)

Reimeus
Reimeus

Reputation: 159784

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

Upvotes: 4

Related Questions