Reputation: 15
How do i group checkboxes in the following codes below? and how can i get the selected value(s) of the checkbox(es) of it in java?
// CHECKBOXES
JCheckBox cb_CCP = new JCheckBox("Chinese Cultural Places");
JCheckBox cb_HandCP = new JCheckBox("Heritage & Culture Places");
JCheckBox cb_HCentres = new JCheckBox("Heritage Centres");
JCheckBox cb_HMuseums = new JCheckBox("Heritage Museums");
JCheckBox cb_ICP = new JCheckBox("Indian Cultural Places");
JCheckBox cb_MCP = new JCheckBox("Malay Cultural Places\r\n");
JCheckBox cb_HTrails = new JCheckBox("Heritage Trails");
JCheckBox cb_PCP = new JCheckBox("Peranakan Cultural Places");
Upvotes: 0
Views: 149
Reputation: 209004
To group, you need a ButtonGroup
ButtonGroup bg = new ButtonGroup();
bg.add(cb_CCP);
bg.add(cb_HandCP);
.. and so on
You add whatever checkboxes you want grouped. By grouping, you make it so that only one can be selected.
To check which one is selected, this should be inside your listener
if (cb_CCP.isSelected()){
String s = cb_CCP.getText();
// do something
} else if (cb_HandCP.isSelected()){
// do something
} else if (.....){
...
}
Upvotes: 1