确定ItemListener中的JCheckBox标识。

huangapple go评论74阅读模式
英文:

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();

huangapple
  • 本文由 发表于 2020年10月12日 11:53:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64311478.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定