jeudi 19 novembre 2015

How to deal with multiple jcheckbox and one jbutton JAVA

For example I have 2 checkbox and 1 button my code would be like this.

private class CheckBoxHandler implements ItemListener
    {
    @Override
    public void itemStateChanged(ItemEvent e) 
    {
        if (chckbxNewCheckBox1.isSelected() && chckbxNewCheckBox2.isSelected())
        {
            checkboxcheck1 = 1;
            checkboxcheck2 = 1;

        }
        else if(chckbxNewCheckBox1.isSelected())
        {
            checkboxcheck1 = 1;

        }
        else if(chckbxNewCheckBox2.isSelected())
        {
            checkboxcheck2 = 1;

        }
    }
    }

  private class ButtonHandler implements ActionListener
  {
    @Override
    public void actionPerformed(ActionEvent e) 
    {
         if (checkboxcheck1 == 1 && checkboxcheck2 == 1)
         {
             textFieldSum.setText(String.valueOf(counter));
             textFieldSum1.setText(String.valueOf(counter1));
         }
         else if(checkboxcheck1 == 1)
            {
             textFieldSum.setText(String.valueOf(counter));
            }
         else if (checkboxcheck2 == 1)
         {
              textFieldSum1.setText(String.valueOf(counter1));
         }
         else
         {
            checkboxcheck1 = 0;
         }
    }   
  }

But then what if I have more than 2 checkbox like 10 or more.It would take forever to to make the if statement in CheckBoxHandler and ButtonHandler. Anybody know how to make it work if I have more than 2 checkbox ? My program is read the file and count the specific character in the file and then display it. The way to display it is click on the checkbox and click the yes button. But it will take forever for me to do the if statement. You guys have any idea? Thanks yall so much for help.




Aucun commentaire:

Enregistrer un commentaire