3

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.

Wilson
  • 8,570
  • 20
  • 66
  • 101
  • 3
    You could give each of those ItemListeners a reference to an object that you create which has two methods; one that increments the number of selected checkboxes, and one that decrements it. Whenever a user selects or deselects a checkbox, you could call the appropiate method of that class. Then you only need to create a method to read out the number from that class, and you're done =) – Bartvbl Apr 06 '12 at 14:49
  • @bartvbl: your comment seems to me to be a valid answer. Why not post it as such so we can up-vote it? – Hovercraft Full Of Eels Apr 06 '12 at 15:02
  • If you just care about the number of selected JCheckBoxes, just define an integer counter and add this if statement for every ItemListener or ActionListener. Every time a check box is selected the counter is incremented, every time a check box is deselected it is decremented. if (jCheckBox1.isSelected()) counter++; else counter--; – Costis Aivalis Apr 06 '12 at 15:32

4 Answers4

6

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • What would my code look like to test all JCheckboxes in an arraylist for whether or not they are selected? I'm not sure how to write out code that will check every element in the AL. – Wilson Apr 06 '12 at 15:09
  • @JesusSqueegee: you would use a simple for loop that gets each JCheckBox instance from the ArrayList, checks of its selected via the `isSelected()` method (get to know the API -- it should be your best friend right now) in an if block, and if true, it increments an int counter variable. Give it a try, and let's see what you come up with. – Hovercraft Full Of Eels Apr 06 '12 at 15:20
  • Thank you so much, but my question is actually even simpler haha. I understand the process but I'm blanking on what the syntax is to test every element in the ArrayList.. – Wilson Apr 06 '12 at 15:22
  • @JesusSqueegee: I thought I just told you this -- you iterate through the ArrayList with a for loop -- it's nothing but the most basic Java. Please clarify -- what exactly confuses you? Please show what you've tried as an edit to your original question. – Hovercraft Full Of Eels Apr 06 '12 at 15:23
  • Edited. The iterator I tried to declare is an HTML document iterator..? – Wilson Apr 06 '12 at 15:32
  • @JesusSqueegee: See also [`CheckABunch`](http://stackoverflow.com/questions/4526779). – trashgod Apr 06 '12 at 17:16
2

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.

2

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--;
    }

} 
}
Nitin Chhajer
  • 2,299
  • 1
  • 24
  • 35
1

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.

Ehsan Khodarahmi
  • 4,772
  • 10
  • 60
  • 87