I have two radio buttons made in java. The problem is they are not linked together i.e. they both can be selected at the same time. How do i achieve link between them?
Asked
Active
Viewed 6,501 times
1
-
3you need to read about radio button groups. BTW What ide are you using? – Nitesh Verma Jul 27 '13 at 17:11
-
Hey, was wondering if the answer below helped you? – JNL Jul 29 '13 at 13:31
2 Answers
3
I think you need this;
//Create three radio buttons
JRadioButton aButton = new JRadioButton("A",true);
JRadioButton bButton = new JRadioButton("B");
JRadioButton cButton = new JRadioButton("C");
//Create a ButtonGroup object, add buttons to the group
ButtonGroup myButtonGroup = new ButtonGroup();
myButtonGroup.add(aButton);
myButtonGroup.add(bButton);
myButtonGroup.add(cButton);
//Display radio buttons
getContentPane().setLayout(new FlowLayout());
getContentPane().add(aButton);
getContentPane().add(bButton);
getContentPane().add(cButton);
setSize(250,100);
setTitle("Swing Radio Buttons");
setVisible(true);
Let me know, if helped.

JNL
- 4,683
- 18
- 29
-2
Set the same name
attribute
Radio example
<input id="hello" type="radio" name="greet">
<label for="hello">hello</label>
<input id="hi" type="radio" name="greet">
<label for="hi">hi</label>
<hr />
<input id="all" type="radio" name="who">
<label for="all">world!</label>
<input id="one" type="radio" name="who">
<label for="one">you</label>

ep0
- 710
- 5
- 13
-
-
-
In that case you're looking for this http://docs.oracle.com/javase/7/docs/api/javax/swing/ButtonGroup.html – ep0 Jul 27 '13 at 17:34