Assaf
Assaf

Reputation: 1124

Java - Shuffle Buttons INSIDE a ButtonGroup

In additoin to my previous question: Java - using a IsSelected function in radioButtons and JPanel

I have a program that adds buttons to button groups and panels.

The problem - shuffling the buttons within the button group.

I am familier with the option to shuffle the button groups, but it does not shuffle the buttons inside the group.

Here's the relevant code:

public void addButtonsToFrame(ArrayList<Question> q, int number_of_lines,
                              ArrayList<ButtonGroup> BG, ArrayList<JPanel> JP) {
    int j = 0;
    for (int i = 0; i < q.size(); i++) {
        qNumber = i + 1;
        BG.add(new ButtonGroup());
        JP.add(new JPanel());
        JP.get(i).setSize(499, 400);
        //creating buttons
        JRadioButton option1 = new JRadioButton(q.get(i).get_option1());
        option1.setActionCommand(q.get(i).get_option1());
        JRadioButton option2 = new JRadioButton(q.get(i).get_option2());
        option2.setActionCommand(q.get(i).get_option2());
        JRadioButton option3 = new JRadioButton(q.get(i).get_option3());
        option3.setActionCommand(q.get(i).get_option3());
        JRadioButton option4 = new JRadioButton(q.get(i).get_option4());
        option4.setActionCommand(q.get(i).get_option4());

        //adding to group buttons
        BG.get(j).add(option1);
        BG.get(j).add(option2);
        BG.get(j).add(option3);
        BG.get(j).add(option4);

        //Collections.shuffle(BG);
        //adding the buttons to the panel
        JP.get(j).add(option1);
        JP.get(j).add(option2);
        JP.get(j).add(option3);
        JP.get(j).add(option4);
        //setting layout that matches our goal
        this.setLayout(new GridLayout(number_of_lines + 1, 1));
        //set title and border for each question
        JP.get(i).setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createEtchedBorder(), "question number " + qNumber + ": " + q.get(i).get_question()));
        //adding the panel to the frame
        this.add(JP.get(j));
        //BG.get(i).getSelection()
        JP.get(j).setVisible(true);
        j++;

    }
}

Upvotes: -2

Views: 171

Answers (1)

Assaf
Assaf

Reputation: 1124

As the fellow user suggested before - I had to add the buttons to an ArrayList before adding the to a panel.

I did that and the program works.

Thanks!

        JRadioButton option1 = new JRadioButton(q.get(i).get_option1());
        option1.setActionCommand(q.get(i).get_option1());
        buttons.add(option1);
        JRadioButton option2 = new JRadioButton(q.get(i).get_option2());
        option2.setActionCommand(q.get(i).get_option2());
        buttons.add(option2);
        JRadioButton option3 = new JRadioButton(q.get(i).get_option3());
        option3.setActionCommand(q.get(i).get_option3());
        buttons.add(option3);
        JRadioButton option4 = new JRadioButton(q.get(i).get_option4());
        option4.setActionCommand(q.get(i).get_option4());
        buttons.add(option4);
        Collections.shuffle(buttons);

Upvotes: 0

Related Questions