英文:
determine identification of a JCheckBox in itemListener
问题
以下是您要翻译的内容:
我有4个复选框,用户可以选择或取消选择它们以进行所需的设置,但是程序要求至少选中一个复选框以生成用户所需的信息。如果用户取消选中最后一个剩余的已选复选框,我希望程序可以为其重新选中它,但为了做到这一点,我需要获取他们选中的最后一个复选框,我应该如何在ItemListener中识别出他们选中了哪个特定的复选框?
private class HandlerClass implements ItemListener {
public void itemStateChanged(ItemEvent e){
if (atLeastOneBoxChecked()){
generationSettings.includeAZLowerCaseChars(azCheck.isSelected());
generationSettings.includeAZUpperCaseChars(AZCheck.isSelected());
generationSettings.include09Chars(o9Check.isSelected());
generationSettings.includeSpecialChars(specialCheck.isSelected());
} else{
// 将未选中的复选框重置为选中状态
}
}
public boolean atLeastOneBoxChecked(){
return azCheck.isSelected() || AZCheck.isSelected() || o9Check.isSelected() || specialCheck.isSelected();
}
}
英文:
I have 4 check boxes, and the user can select or deselect them for their required settings, however, the program requires at least 1 check box to be checked in order to generate the required information for the user. If the user unchecks the last remaining checked box, I want the program to recheck it for them, but in order to do that I need to get the last checkbox that they checked, how might I go about identifying which specific checkbox they checked within the itemListener?
private class HandlerClass implements ItemListener {
public void itemStateChanged(ItemEvent e){
if (atLeastOneBoxChecked()){
generationSettings.includeAZLowerCaseChars(azCheck.isSelected());
generationSettings.includeAZUpperCaseChars(AZCheck.isSelected());
generationSettings.include09Chars(o9Check.isSelected());
generationSettings.includeSpecialChars(specialCheck.isSelected());
} else{
// reset unchecked box to checked
}
}
public boolean atLeastOneBoxChecked(){
return azCheck.isSelected() || AZCheck.isSelected() || o9Check.isSelected() || specialCheck.isSelected();
}
}
答案1
得分: 1
The getSource()
method of the ItemEvent
will contain the check box:
JCheckBox checkBox = (JCheckBox)e.getSource();
英文:
> how might I go about identifying which specific checkbox they checked within the itemListener?
The getSource()
method of the ItemEvent
will contain the check box:
JCheckBox checkBox = (JCheckBox)e.getSource();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论