Preventing multiples selection of checkboxes per radio buttons
Hi guys,
I am messing around with checkboxes and thought of something interesting. Hopefully, it can be done.
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
publicclass myProgram1extends JFrame
{
privatestaticfinallong serialVersionUID = 1;
static JFrame frame =new JFrame();
publicstaticvoid main(String args[])
{
frame.setTitle("Checkboxes");
JPanel mainPanel =new JPanel();
JScrollPane scrollPane =new JScrollPane(mainPanel);
mainPanel.setLayout(new GridBagLayout());
GridBagConstraints a =new GridBagConstraints();
JRadioButton b1 =new JRadioButton("Joe");
JRadioButton b2 =new JRadioButton("Smith");
JCheckBox chkcoach =new JCheckBox("Coach");
JCheckBox chkplayer =new JCheckBox("Player");
ButtonGroup group =new ButtonGroup();
group.add(b1);
group.add(b2);
a.insets =new Insets(0, 10, 8, 0);
a.gridx = 0;
a.gridy = 1;
a.anchor = GridBagConstraints.WEST;
mainPanel.add(b1, a);
a.gridx = 1;
mainPanel.add(b2, a);
a.insets =new Insets(0, 10, 8, 0);
a.gridx = 0;
a.gridy = 2;
a.anchor = GridBagConstraints.WEST;
mainPanel.add(chkcoach, a);
a.gridx = 1;
mainPanel.add(chkplayer, a);
frame.add(scrollPane);
frame.pack();
frame.setSize(new Dimension(300,200));
frame.setLocation(200,200);
frame.setVisible(true);
}
}
If Joe is checked as the coach, Smith can't be the coach. Joe can be coach and player and Smith is only a player. If Smith is checked as the coach, then Joe can't be the coach.
If Joe is currently coach and Smith has to be coach, the coach checkbox for Joe will have to be unchecked first.
Is there a way to enforce this scenario ?
Do please let me know and I appreciate the responses.

