[英]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.我的JFrame中有11个不同的复选框,并且希望在检查一个总数时能够获得一个数字。 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..
我知道如何设置一个 ItemListener 并查看是否检查了一个,但我不确定如何检查所有这些......
EDIT:编辑:
cblist is an ArrayList containing 11 JCheckBoxes. cblist 是一个包含 11 个 JCheckBox 的 ArrayList。 I gave every JCheckBox an item listener and hereis the class used when the checkboxes are clicked...
我给每个 JCheckBox 一个项目侦听器,这里是单击复选框时使用的 class ...
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.在 for 循环中,我如何测试 ArrayList 的所有元素。我知道我的语法现在不正确。
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.一种方法:将所有 JCheckBoxes 放入数组或
ArrayList<JCheckBox>
中,并在需要时简单地遍历列表以查看选中了哪些复选框。
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.另一种可能的解决方案:如果您有表格结构,请使用 JTable 在其 model 中保存布尔值,然后在需要时遍历 TableModel 的行以查看哪些行保存 Boolean.TRUE 值。
My proposal (maybe not the best) is to keep all checked CheckBox in a List.我的建议(也许不是最好的)是将所有选中的 CheckBox 保存在一个列表中。
So to listener for all JCheckBoxex will be like this:所以对于所有 JCheckBoxex 的监听器将是这样的:
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.问候。
you can keep a global counter countChecked
and make the frame implements ItemListener
您可以保留一个全局计数器
countChecked
并使框架implements ItemListener
for all the JCheckBox
in your frame chkBox.addItemListener(this)
and handle the events对于框架中的所有
JCheckBox
chkBox.addItemListener(this)
并处理事件
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--;
}
}
}
add "ActionPerformed" event listener for all of your checkboxes & call this method inside event handler method to get number of checked checkboxes:为所有复选框添加“ActionPerformed”事件侦听器并在事件处理程序方法中调用此方法以获取选中复选框的数量:
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. getRootPane 应该返回您的主面板上有哪些组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.