0

So I'm trying to make it so that my JList consists of random values everytime I boot my program up (I was using a string array to randomize values from 2 different arrays). I have been struggling to get this working. The elements show when I use one array, but it doesn't work when I use the new array that consists of a string with 2 values from those 2 arrays. How can I make it so that the array with both random values from 2 arrays show up on my JList. I have tried everything starting with making a new array to using DeaultListModel. Any help is appreciated.

Here is my code:

public class game extends JFrame {

    private JPanel contentPane;
    private JTextField textField_1;
    private JTextField textField_2;

    //\u21BA is Skip, \u20E0 is Reverse. Action and colorName are the arrays I'm randomizing into the JList
    private String[] action = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+2",  "\u21BA", " \u20E0"};
    private Color[] color = {Color.red, Color.blue, Color.green, Color.yellow};
    private String[] colorName = {"Red", "Blue", "Green", "Yellow"};
    private DefaultListModel deck;
    /**
     * Launch the application.
     */
    public static void main (String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    game frame = new game();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public game() {
        setTitle("Uno");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 500);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout(0, 0));

        JTextArea textArea = new JTextArea();
        textArea.setRows(5);
        contentPane.add(textArea, BorderLayout.SOUTH);
        \\What I'm struggling with 
        deck = new DefaultListModel();
        for (int i = 0; i <= 7; i++) {
            Random r = new Random();
            deck.addElement(colorName[r.nextInt(colorName.length)] + " " + action[r.nextInt(action.length)]);
        }
        JList list = new JList(deck);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setFont(UIManager.getFont("ColorChooser.font"));
        contentPane.add(list, BorderLayout.WEST);

        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.EAST);
        GridBagLayout gbl_panel = new GridBagLayout();
        gbl_panel.columnWidths = new int[] {44, 0};
        gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0};
        gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
        gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
        panel.setLayout(gbl_panel);

        JLabel lblNewLabel = new JLabel("Player 1 Card Amount");
        lblNewLabel.setFont(new Font("Dialog", Font.PLAIN, 12));
        GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
        gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0);
        gbc_lblNewLabel.gridx = 0;
        gbc_lblNewLabel.gridy = 3;
        panel.add(lblNewLabel, gbc_lblNewLabel);

        textField_1 = new JTextField();
        textField_1.setEditable(false);
        textField_1.setFont(new Font("Dialog", Font.PLAIN, 12));
        GridBagConstraints gbc_textField_1 = new GridBagConstraints();
        gbc_textField_1.insets = new Insets(0, 0, 5, 0);
        gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField_1.gridx = 0;
        gbc_textField_1.gridy = 4;
        panel.add(textField_1, gbc_textField_1);
        textField_1.setColumns(10);

        JLabel lblNewLabel_1 = new JLabel("CPU Card Amount");
        lblNewLabel_1.setFont(new Font("Dialog", Font.PLAIN, 12));
        GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
        gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 0);
        gbc_lblNewLabel_1.gridx = 0;
        gbc_lblNewLabel_1.gridy = 6;
        panel.add(lblNewLabel_1, gbc_lblNewLabel_1);

        textField_2 = new JTextField();
        textField_2.setFont(new Font("Dialog", Font.PLAIN, 12));
        textField_2.setEditable(false);
        GridBagConstraints gbc_textField_2 = new GridBagConstraints();
        gbc_textField_2.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField_2.gridx = 0;
        gbc_textField_2.gridy = 7;
        panel.add(textField_2, gbc_textField_2);
        textField_2.setColumns(10);
    }

}
6
  • 1
    You certainly shouldn't be creating a new Random every time around the loop. Are there really only seven cards in the deck? It sounds like that's the player's hand. It doesn't make sense to me to create random cards, here. They could all be Red 5's. I would create a deck, shuffle it with Fisher-Yates (Collections.shuffle), and take the first seven cards for the player's hand. Commented Jun 12, 2020 at 23:46
  • 1
    JList and DefaultListModel are both generic types. They should be JList<String> and DefaultListModel<String> in this case, although that isn't causing your problem. Also, the normal convention for Java is for class names to be capitalized (Game instead of game). Commented Jun 12, 2020 at 23:49
  • I meant that the player holds 7 cards. The actual deck has a lot of cards. Sorry for the confusion. Commented Jun 12, 2020 at 23:58
  • If that variable is meant to hold the player's hand, and not the deck, then calling it deck could definitely be confusing. ;) Commented Jun 13, 2020 at 0:01
  • I got it to work by doing what you mentioned. Ty Commented Jun 13, 2020 at 2:04

1 Answer 1

1

JList and DefaultListModel are both generic types. They should be JList<String> and DefaultListModel<String> in this case, although that isn't causing your problem. Also, the normal convention for Java is for class names to be capitalized (Game instead of game).

You certainly shouldn't be creating a new Random every time around the loop. It doesn't make sense to me to create random cards, here. They could all be Red 5's. I would create a deck, shuffle it with Fisher-Yates (Collections.shuffle), and take the first seven cards for the player's hand.

If that variable is meant to hold the player's hand, and not the deck, then calling it deck could definitely be confusing. It sounds like that's the player's hand. Better to call it hand.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.