a JRadioButton in more ButtonGroups
Hi guys. I have 4 radioButtons and I am trying to understand how to permit only to two of them at a time to be selected.
ButtonGroup buttonGroupFive=new ButtonGroup();
ButtonGroup buttonGroupOne=new ButtonGroup();
ButtonGroup buttonGroupTwo=new ButtonGroup();
ButtonGroup buttonGroupSix=new ButtonGroup();
buttonGroupFive.add(jRadioButton1);
buttonGroupFive.add(jRadioButton3);
buttonGroupSix.add(jRadioButton4;
buttonGroupSix.add(jRadioButton2);
buttonGroupOne.add(jRadioButton1);
buttonGroupOne.add(jRadioButton2);
buttonGroupTwo.add(jRadioButton3);
buttonGroupTwo.add(jRadioButton4);
I want that they are selected oppositely. For instance:
if jRadioButton1 is selected, then the opposite jRadioButton4 must be selected. But jRadioButton2 and jRadioButton3 must be deselected
if jRadioButton2 is selected, then the opposite jRadioButton3 must be selected.But jRadioButton1 and jRadioButton4 must be deselected
Why doesn't it work? Is there another solution? Thanks!
[1203 byte] By [
albertthea] at [2007-11-27 4:35:13]

# 1
You don't need so many ButtonGroups to do this. Only the two (radio buttons 1 and 4 in one, 2 and 3 in the other).
The trick is to add an ItemListener to each button.
This must implement the method itemStateChanged(ItemEvent).
The affected button can be given by ItemEvent.getItem(), and whether it has been selected or de-selected is given by ItemEvent.getStateChange().
From this, you can check when one of your radio buttons has been selected, and select the corresponding button in the other group, either using JRadioButton.setSelected(true) or ButtonGroup.setSelected(true).
# 3
Ah, you're perfectly correct, I hadn't thought of that drawback!
I think you can work around this - you have to check which buttons are selected before you change the selection. For example, if you click on button 1, the listener will make sure that button 4 is selected, which will alert the listener again, but this time the listener won't make any changes because button 1 is already selected. This should stop an infinite loop from happening.
# 4
Yes, clairec666's suggestion has a typo that causes a loop between the items: If your description is correct, then Button 1 and 2 must be in one ButtonGroup, and Button 3 and 4 in another (alternatively 1 + 3 and 2 + 4). In any case, buttons 1 and 4 must be in separate button groups, and buttons 2 and 3 too.
This way, when you click button 1, three things will happen (assuming the distribution is 1+2 and 3 + 4):
1. Button 2 will be deactivated, since it is in the same group as Button 1 (that's the purpose of a ButtonGroup in the first place)
2. The ItemListener of Button 1 will select Button 4 - that's what you want
3. Selecting Button 4 will cause Button 3 to be deselected, since it is in the same group as Button 4
This is the behavior you want, isn't it?
Greets,
Mike
# 5
you only need a single button group, and work the models
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Testing
{
public void buildGUI()
{
JRadioButton rb1 = new JRadioButton("RB1",true);
JRadioButton rb2 = new JRadioButton("RB2");
JRadioButton rb3 = new JRadioButton("RB3");
JRadioButton rb4 = new JRadioButton("RB4");
ButtonGroup group = new ButtonGroup();
group.add(rb1); group.add(rb2);
rb4.setModel(rb1.getModel());
rb3.setModel(rb2.getModel());
JPanel p = new JPanel(new GridLayout(4,1));
p.add(rb1);p.add(rb2);p.add(rb3);p.add(rb4);
JFrame f = new JFrame();
f.getContentPane().add(p);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new Testing().buildGUI();
}
});
}
}