Skip to content
Advertisement

determine identification of a JCheckBox in itemListener

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

Advertisement

Answer

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();
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement