Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
812 views
in Technique[技术] by (71.8m points)

java - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...