With multiple yes/no radio buttons, do I have to create each instance?

74 Views Asked by At

I'm trying to make an app with a panel where it asks a question (whether an image contains humans, cars, or other objects... and more). Each question is answered with yes/no. I didn't want to make JRadioButton for each of the 'yes's and 'no's. So I tried to add the same Radio Button multiple times but it doesn't work (See below).

contentsPanel = new JPanel();
contentsHumans = new JLabel("Humans? ");
contentsCars = new JLabel("Cars? ");
contentsOtherObjects = new JLabel("Other Objects? ");

yes = new JRadioButton("Yes"); 
no = new JRadioButton("No");
binaryAnswer = new ButtonGroup();
binaryAnswer.add(yes);
binaryAnswer.add(no);

contentsPanel.setBorder(BorderFactory.createTitledBorder(
                 BorderFactory.createEtchedBorder(), "Image contains... "));
contentsPanel.add(contentsHumans);
contentsPanel.add(yes); contentsPanel.add(no);
contentsPanel.add(contentsCars);
contentsPanel.add(yes); contentsPanel.add(no);
contentsPanel.add(contentsOtherObjects);
contentsPanel.add(yes); contentsPanel.add(no);

Do I have to separately create y1 = new JRadioButton("Yes");, y2 = new JRadioButton("Yes");, etc., for all my 'yes's and 'no's?? I can definitely copy and paste to do that but I was wondering if there is any other way.

Here's the image of current output:

0

There are 0 best solutions below