Checkbox - how to check if at least 1 cbx was selected

0

Hello, I have a set of 8 checkBoxs. Does anyone know how I can check if at least 1 of them has been selected? Thank you

    
asked by anonymous 02.12.2016 / 19:55

2 answers

1

If you want to test one-by-one:

boolean algumSelecionado = jcbx1.isSelected() || jcbx2.isSelected() || jcbx3.isSelected() || jcbx4.isSelected()
                        || jcbx5.isSelected() || jcbx6.isSelected() || jcbx7.isSelected() || jcbx8.isSelected();

Or a more dynamic solution if JCheckBox is inside a JPanel:

boolean algumSelecionado = false;

for (Component c : jpnl.getComponents()) {
  if (c instanceof JCheckBox && ((JCheckBox) c).isSelected()) {
    algumSelecionado = true;
    break;
  }
}
    
02.12.2016 / 20:00
1
CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
checkBox1.isChecked();
    
02.12.2016 / 20:02