2

I want to have a random order for displaying the cards or screens in my CardLayout. I need guidance on how to accomplish this. What is strategy I should use?

I tried using the code below, but it is in a fixed order. I want to be able to choose whichever order I like.

EDIT !

Sorry, by random order I did not mean shuffling. But, it is good to know. I want the user of the program to be able to enter some input. Depending on the value of the input, a particular screen/card is displayed.

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

public class CardLayoutExample extends JFrame {

private int currentCard = 1;
private JPanel cardPanel;
private CardLayout cl;

public CardLayoutExample() {

    setTitle("Card Layout Example");
    setSize(300, 150);
    cardPanel = new JPanel();

    cl = new CardLayout();
    cardPanel.setLayout(cl);
    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    JPanel p3 = new JPanel();
    JPanel p4 = new JPanel();
    JLabel lab1 = new JLabel("Card1");
    JLabel lab2 = new JLabel("Card2");
    JLabel lab3 = new JLabel("Card3");
    JLabel lab4 = new JLabel("Card4");
    p1.add(lab1);
    p2.add(lab2);
    p3.add(lab3);
    p4.add(lab4);

    cardPanel.add(p1, "1");
    cardPanel.add(p2, "2");
    cardPanel.add(p3, "3");
    cardPanel.add(p4, "4");
    JPanel buttonPanel = new JPanel();
    JButton b1 = new JButton("Previous");
    JButton b2 = new JButton("Next");
    buttonPanel.add(b1);
    buttonPanel.add(b2);
    b1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if (currentCard > 1) {
                currentCard -= 1;
                cl.show(cardPanel, "" + (currentCard));
            }
        }
    });

    b2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if (currentCard < 4) {
                currentCard += 1;
                cl.show(cardPanel, "" + (currentCard));
            }
        }
    });
    getContentPane().add(cardPanel, BorderLayout.NORTH);
    getContentPane().add(buttonPanel, BorderLayout.SOUTH);
}

public static void main(String[] args) {
    CardLayoutExample cl = new CardLayoutExample();
    cl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    cl.setVisible(true);
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Korg
  • 93
  • 2
  • 9
  • 1
    What have you tried? Where are you stuck? Do you have an example of what you've done so far? – brbcoding Mar 07 '13 at 23:01
  • 1
    @brbcoding - added the code i was using. – Korg Mar 07 '13 at 23:04
  • *"I want the user of the program to be able to enter some input."* Where is the part of the code where you prompt the user for a card number? What is your specific question? – Andrew Thompson Mar 08 '13 at 11:53
  • @AndrewThompson - The user enters these things through a key board. The keyboard is not ready yet. But I want to make this code ready so that I can simply add a keyboard here and get started. – Korg Mar 09 '13 at 06:58

2 Answers2

4

Put the CartLayouts in a List, shuffle the List, add to the containing layout in the List order.

Skip Head
  • 7,580
  • 1
  • 30
  • 34
  • @trashgod and skip head - Sorry, by random order I did not mean shuffling. But, it is good to know. I want the user of the program to be able to enter some input. Depending on the value of the input, a particular screen/card is displayed. – Korg Mar 07 '13 at 23:21
  • @Korg maybe then you want a `JTabbedPane`. `JTabbedPane` is like a CardLayout with tab headers to switch from one pane to another (user input would here be to click on the header). – Guillaume Polet Mar 07 '13 at 23:32
  • @GuillaumePolet - the card shows a menu (button, check box, slider, Anything). The user selects the menu. Then the right card for that menu is displayed. Thats what i want to do. – Korg Mar 08 '13 at 00:01
  • @Korg: This [example](http://stackoverflow.com/a/5655843/230513) demonstrates `Action`, which can be used in menus. – trashgod Mar 08 '13 at 00:15
  • @trashgod - nice example. thanks. I want to use a keypad to control which panel is displayed instead of previous and next button. Can I just replace those buttons with my keypad? Are there any things i need to be careful about ? – Korg Mar 08 '13 at 06:43
  • @Korg: An `Action` can be used by any button. – trashgod Mar 08 '13 at 07:09
3

Here is a simple way to jump directly to a card.

final JButton jumpTo = new JButton("Jump To");
buttonPanel.add(jumpTo);
jumpTo.addActionListener( new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent ae) {
        String[] names = {"1","2","3","4"};
        String s = (String)JOptionPane.showInputDialog(
            jumpTo,
            "Jump to card",
            "Navigate",
            JOptionPane.QUESTION_MESSAGE,
            null,
            names,
            names[0]);
        if (s!=null) {
            cl.show(cardPanel, s);
        }
    }
} );

Obviously this will require some changes to the rest of the code. Here is an SSCCE.

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

public class CardLayoutExample extends JFrame {

private int currentCard = 1;
private JPanel cardPanel;
private CardLayout cl;

public CardLayoutExample() {

    setTitle("Card Layout Example");
    setSize(300, 150);
    cardPanel = new JPanel();

    cl = new CardLayout();
    cardPanel.setLayout(cl);
    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    JPanel p3 = new JPanel();
    JPanel p4 = new JPanel();
    JLabel lab1 = new JLabel("Card1");
    JLabel lab2 = new JLabel("Card2");
    JLabel lab3 = new JLabel("Card3");
    JLabel lab4 = new JLabel("Card4");
    p1.add(lab1);
    p2.add(lab2);
    p3.add(lab3);
    p4.add(lab4);

    cardPanel.add(p1, "1");
    cardPanel.add(p2, "2");
    cardPanel.add(p3, "3");
    cardPanel.add(p4, "4");
    JPanel buttonPanel = new JPanel();
    JButton b1 = new JButton("Previous");
    JButton b2 = new JButton("Next");
    buttonPanel.add(b1);
    buttonPanel.add(b2);
    b1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if (currentCard > 1) {
                currentCard -= 1;
                cl.show(cardPanel, "" + (currentCard));
            }
        }
    });

    b2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if (currentCard < 4) {
                currentCard += 1;
                cl.show(cardPanel, "" + (currentCard));
            }
        }
    });

    final JButton jumpTo = new JButton("Jump To");
    buttonPanel.add(jumpTo);
    jumpTo.addActionListener( new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent ae) {
            String[] names = {"1","2","3","4"};
            String s = (String)JOptionPane.showInputDialog(
                jumpTo,
                "Jump to card",
                "Navigate",
                JOptionPane.QUESTION_MESSAGE,
                null,
                names,
                names[0]);
            if (s!=null) {
                cl.show(cardPanel, s);
            }
        }
    } );

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

public static void main(String[] args) {
    CardLayoutExample cl = new CardLayoutExample();
    cl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    cl.setVisible(true);
}
}

BTW - my comment "Where is the part of the code where you prompt the user for a card number?" was actually a very subtle way to try & communicate.. For better help sooner, post an SSCCE.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433