如何计算已检查的JCheckbox的数量? [英] How to count number of JCheckboxes checked?

查看:49
本文介绍了如何计算已检查的JCheckbox的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的JFrame中有11个不同的复选框,并且希望能够在每次检查一个总数时得到一个数字.我知道如何设置ItemListener并查看是否选中了它,但是我不确定如何检查所有这些..

cblist是一个包含11个JCheckBox的ArrayList.我给每个JCheckBox一个项目侦听器,这是单击复选框时使用的类...

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

      }
  }

在for循环中,如何测试ArrayList的所有元素.我知道我的语法现在不正确.

解决方案

一种方法:将所有JCheckBoxes放入数组或ArrayList<JCheckBox>中,并在需要时简单地遍历列表以查看选中了哪些复选框. /p>

另一种可能的解决方案:如果您具有表格结构,请使用在其模型中包含布尔值的JTable,然后在需要时遍历TableModel的行以查看哪些行包含Boolean.TRUE值.

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.

解决方案

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.

这篇关于如何计算已检查的JCheckbox的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆