How to show different cards in a CardLayout?
Asked Answered
B

2

8

I looked at a code example that used this code:

cl.show(cardPanel, "" + (currentCard));

But when I use show I get a message in Eclipse that it's deprecated and I wonder if there is another way to show the different cards in the CardLayout when I click on the buttons? Below is the code for my CardLayout class. Suggestions are also welcome if some part of the code are bad practise. Thanks!

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class CardLayoutTest extends JFrame implements ActionListener {

// Ref
 private JPanel cardPanel, jp1, jp2, buttonPanel;
 private JLabel jl1, jl2;
 private JButton btn1, btn2;
 private CardLayout cardLayout;

// Konstruktor
 public CardLayoutTest()
 {
     setTitle("Test med CardLayout");
     setSize(600,400);

     cardPanel = new JPanel();
     buttonPanel = new JPanel();

     cardPanel.setLayout(cardLayout);

     jp1 = new JPanel();
     jp2 = new JPanel();

     jl1 = new JLabel("Card 1");
     jl2 = new JLabel("Card 2");

     jp1.add(jl1);
     jp2.add(jl2);

     cardPanel.add(jp1, "1");
     cardPanel.add(jp2, "2");

     btn1 = new JButton("Show Card 1");
     btn2 = new JButton("Show Card 2");

     buttonPanel.add(btn1);
     buttonPanel.add(btn2);

     getContentPane().add(cardPanel, BorderLayout.NORTH);
     getContentPane().add(buttonPanel, BorderLayout.SOUTH);

     btn1.addActionListener(this);
 }

     public void actionPerformed(ActionEvent event)
     {
        // ??? Show card 1 ???

        // ??? Show card 2 ???
     }

 public static void main(String[] args) {
     CardLayoutTest frame = new CardLayoutTest();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    } 
}
Buccal answered 30/5, 2012 at 19:59 Comment(0)
V
17

EDIT (I tried your code example)

1.you forgot to initialize most important variable

private CardLayout cardLayout = new CardLayout();

2.then SSCCE could be

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class CardLayoutTest extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel cardPanel, jp1, jp2, buttonPanel;
    private JLabel jl1, jl2;
    private JButton btn1, btn2;
    private CardLayout cardLayout = new CardLayout();

    public CardLayoutTest() {
        setTitle("Test med CardLayout");
        setSize(400, 300);
        cardPanel = new JPanel();
        buttonPanel = new JPanel();
        cardPanel.setLayout(cardLayout);
        jp1 = new JPanel();
        jp2 = new JPanel();
        jl1 = new JLabel("Card 1");
        jl2 = new JLabel("Card 2");
        jp1.add(jl1);
        jp2.add(jl2);
        cardPanel.add(jp1, "1");
        cardPanel.add(jp2, "2");
        btn1 = new JButton("Show Card 1");
        btn1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                cardLayout.show(cardPanel, "1");
            }
        });
        btn2 = new JButton("Show Card 2");
        btn2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                cardLayout.show(cardPanel, "2");
            }
        });
        buttonPanel.add(btn1);
        buttonPanel.add(btn2);
        add(cardPanel, BorderLayout.NORTH);
        add(buttonPanel, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                CardLayoutTest frame = new CardLayoutTest();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}
Vie answered 30/5, 2012 at 20:7 Comment(1)
Thanks for the new code! It's working good! But I have some questions about some lines that you added, like this one: private static final long serialVersionUID = 1L; I can see that i removed a yellow alert triangle in Eclipse, but what is it doing? And you also replaced this line: getContentPane().add(cardPanel, BorderLayout.NORTH); whith this: add(cardPanel, BorderLayout.NORTH); whats the difference? And finally could you also be kind and explain what you have done in the main method? Thanks!Buccal
B
9

It is most likely that you are calling show() on the JPanel, rather than the CardLayout.

The method show() used to exist in JPanel (more specifically Component) and has been replaced by setVisible(). This is completely different from the show() method of CardLayout.

Make sure you are doing something like the following in your action listener

CardLayout cardLayout = (CardLayout)(cardPanel.getLayout());
cardLayout.show(cardPanel, "CardToShow");

As a side note, it doesnt appear you are "newing" your CardLayout. Make sure you do that.

Barrettbarrette answered 30/5, 2012 at 20:17 Comment(1)
Yes I missed "newing" my CardLayout and I also made some misstake with show! Thanks!Buccal

© 2022 - 2024 — McMap. All rights reserved.