-2

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++;

    }
}
Assaf
  • 1,112
  • 4
  • 14
  • 35
  • 1
    (1-) You were asked in one of your previous question to follow Java variable naming conventions. People in the forum who answer questions expect these conventions to be followed since it makes the code easier to read and understand. You were also asked to post an [mcve] with your question. You haven't listened to these simple suggestions, so I'll skip this question. – camickr Nov 29 '17 at 16:08
  • I certainly understand and you are right, however I tried (and succeeded, with the kind help of a fellow user) to get my code working. I will fix that and try harder in the future, but I have to serve my assignment soon. Thank you! – Assaf Nov 29 '17 at 16:10
  • @camickr I don't understand why I have to post a whole bunch of code just for this question, but I will make a reference to my previous question which was MCVE. – Assaf Nov 29 '17 at 16:11
  • Because the whole point of creating an `MCVE` is for you to simplify the code and the question to make sure you are understanding what you are asking. Your question is about shuffling buttons. So all you need is a frame with a few buttons and a button to shuffle the button. If you can't simplify the code to only the code directly related to the question then it means you don't understand the question. The time requirements of your assignment are irrelevant to the question. We are not here to do your homework for you. – camickr Nov 29 '17 at 16:15
  • I'll keep that in mind - thanks. – Assaf Nov 29 '17 at 16:16
  • 1
    In any case, components are displayed in the order in which they are added to the panel. So you need to shuffle the buttons BEFORE adding them to the panel. The ButtonGroup is irrelevant for the order in which a component is displayed. – camickr Nov 29 '17 at 16:18
  • @camickr Thank you - solved the problem by adding the buttons to ArrayList before adding them to the Panel. I will pay attention to convetions as you suggested! – Assaf Nov 29 '17 at 16:29

1 Answers1

0

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);
Assaf
  • 1,112
  • 4
  • 14
  • 35