Checkbox - how to check if at least 1 cbx has been selected

Asked

Viewed 349 times

0

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

2 answers

1


If you want to try one-on-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 are inside a Jpanel:

boolean algumSelecionado = false;

for (Component c : jpnl.getComponents()) {
  if (c instanceof JCheckBox && ((JCheckBox) c).isSelected()) {
    algumSelecionado = true;
    break;
  }
}
  • Simple and Brilliant! Thanks! I only changed . isSelected by . isChecked.

  • @Elainebredaschwaner is not swing? You have to see if the second option I put works then

  • I don’t know what swing is. But it’s working, wonderful!

1

CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
checkBox1.isChecked();
  • Thank you. I added your answer to the colleague’s and gave everything wonderful.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.