Fire Lancer
Fire Lancer

Reputation: 21

How do I make a CardLayout work with an arbitrary amount of cards?

I am trying to make cardLayout work with an arbitrary amount of cards, meaning I will need some kind of a loop through all the objects I have. Now I tried and I made it work with manually created JPanels but once I put a loop in it doesn't work.

@SuppressWarnings("serial")

public class ClassCardLayoutPane extends JPanel{

JPanel cards;

public ClassCardLayoutPane() {
    initialiseGUI();
}

private void initialiseGUI() {
    String[] listElements = {"A2", "C3"};
    cards = new JPanel(new CardLayout());


    JLabel label = new JLabel("Update");
    add(label);

    JList selectionList = new JList(listElements);
    selectionList.addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent evt) {

            if (!evt.getValueIsAdjusting()) {
                label.setText(selectionList.getSelectedValue().toString());
                CardLayout cl = (CardLayout)(cards.getLayout());
                cl.show(cards, label.getText());


            }
        }

    });

    // The panels created by this loop don't work, the cards get stuck on the first one

    /*
    for (int i = 0; i < listElements.length-1; i ++) {

        JPanel temp = new JPanel();
        temp.add(new JLabel(i+""));
        cards.add(temp, listElements[i]);

    }*/


    JPanel card1 = new JPanel();
    card1.add(new JTable(20,20));

    JPanel card2 = new JPanel();
    card2.add(new JTable(10,20));

    cards.add(card1, listElements[0]);
    cards.add(card2, listElements[1]);

    //the panels here do work. I don't know what I'm doing wrong

    add(selectionList);
    add(cards);
}

public static void main(String[] args) {
    JFrame main = new JFrame("Win");
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    main.setPreferredSize(new Dimension(1366, 768));
    main.getContentPane().add(new ClassCardLayoutPane());
    main.pack();
    main.setVisible(true);
}

}

Okay so the commented out for loop is what doesn't work for me which has me really confused? Can someone explain to me why it doesn't work and how I could make it work? By the way, listElements can be a different size, that's what I'm trying to get working because eventually, listElements will start off as a LinkedList, so when I create the array for the ListItems, it will be a different size every time because I don't know how many items there will be. Can someone please help me make this work? By "It doesn't work", I mean when I use the loop, the JPanel gets stuck on the very first card and doesn't switch to the next card anymore! There is no error message, the program runs fine but it doesn't do what it's meant to do which is switch cards! Note that when I do them individually, the program works perfectly! Thank you.

Upvotes: 0

Views: 48

Answers (1)

camickr
camickr

Reputation: 324128

Okay so the commented out for loop is what doesn't work for me which has me really confused?

Well the first thing you should be doing is adding debug code to the loop to see if unique card names are being used.

If you did that then you would notice the following problem:

for (int i = 0; i < listElements.length-1; i ++) {

Why are you subtracting 1 fro the list length? You only ever add one panel to the CardLayout.

The code should be:

for (int i = 0; i < listElements.length; i ++) {

When code doesn't execute as you think you need to add debug code or use a debugger to step through the code to see if the code executes as you expect.

You can't just always stare at the code.

Upvotes: 1

Related Questions