Wilson
Wilson

Reputation: 8768

How to count number of JCheckboxes checked?

I have 11 different checkboxes in my JFrame and want to be able to get a number whenever one is checked for how many total are checked. I know how to set up an ItemListener and see if one is checked, but I am not sure how I could check all of them..

EDIT:

cblist is an ArrayList containing 11 JCheckBoxes. I gave every JCheckBox an item listener and hereis the class used when the checkboxes are clicked...

private class CheckClass implements ItemListener{
      public void itemStateChanged(ItemEvent event){
         for(cblist.isChecked){
             ingnum++;
         }

      }
  }

In the for loop, how do I test all elements of the ArrayList..I understand my syntax is not correct right now.

Upvotes: 3

Views: 5855

Answers (4)

Ehsan Khodarahmi
Ehsan Khodarahmi

Reputation: 4922

add "ActionPerformed" event listener for all of your checkboxes & call this method inside event handler method to get number of checked checkboxes:

int countCheckedCheckBoxes(){
    Component[] cs = getRootPane().getComponents();
    int checkNums = 0;
    for(Component c : cs){
        if(c instanceof JCheckBox){
            if(((JCheckBox)c).isSelected()){
                checkNums++;
            }
        }
    }
    return checkNums;
}

getRootPane should return your main panel which components are located on it.

Upvotes: 1

Nitin Chhajer
Nitin Chhajer

Reputation: 2339

you can keep a global counter countChecked and make the frame implements ItemListener

for all the JCheckBox in your frame chkBox.addItemListener(this) and handle the events

public class MyFrame extends JFrame implements ItemListener{

private int countChecked = 0;
private JPanel contentPane;
    public MyFrame() {
    contentPane = new JPanel();
    setContentPane(contentPane);
    JCheckBox chckbx = new JCheckBox("New check box");
    contentPane.add(chckbx, BorderLayout.CENTER);
    chckbx.addItemListener(this);
}

@Override
public void itemStateChanged(ItemEvent ie) {
    if(ie.getSource().getClass() == JCheckBox.class)
    {
        if(ie.getStateChange() == ie.SELECTED)
            countChecked++;
        else if(ie.getStateChange() == ie.DESELECTED)
            countChecked--;
    }

} 
}

Upvotes: 2

user973999
user973999

Reputation:

My proposal (maybe not the best) is to keep all checked CheckBox in a List.

So to listener for all JCheckBoxex will be like this :

void stateChanged(ChangeEvent e){
    if( CheckBox is checked){
       // add the checkbox in the list.
    } else {
        // remove CheckBox in the list.
     }
}

To know how many checkBox are checked, just count the size of the list.

Regards.

Upvotes: 2

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

One way: put all of the JCheckBoxes in an array or ArrayList<JCheckBox> and when desired, simply iterate through the list to see which check boxes are selected.

Another possible solution: if you have a tabular structure, use a JTable that holds Booleans in its model, then when desired iterate through the rows of the TableModel to see which rows hold Boolean.TRUE values.

Upvotes: 6

Related Questions