The purpose of ButtonGroup
is multiple-exclusive selection. Do not create ButtonGroup
only if you want to have a collection of your buttons. Instead of ButtonGroup
use a standard collection like ArrayList
.
List<JCheckBox> buttons = new ArrayList<>();
buttons.add(c1);
buttons.add(c2);
buttons.add(c3);
...
for ( JCheckbox checkbox : buttons ) {
if( checkbox.isSelected() )
{
SelectedItem += btn.getText() + ",";
}
}
Further notices: do updates (.setText
) in Swing event thread (invokelater
), remeber that it is better to create StringBuilder in such concatenation, but with UI component quantities like this, performance impact propably will be not noticeable.