jeudi 3 mars 2016

Switch case with Checkboxes

I have a switch case statement that is supposed to get the selected index of a combo box and create checkboxes for objects in an ArrayList that the selected index is associated with.

On start up there are no check boxes present, and when a combo box item is selected nothing changes.

  int index = showlist.getSelectedIndex();
  switch (index)
  {
     case 1: 
         for(i=0;i<studentlist1.size();i++)
        {
            JCheckBox chbx = new JCheckBox(studentlist1.get(i).getName());
            panel.add(chbx);

            chbx.addChangeListener(new ChangeListener()
            {
                @Override
                public void stateChanged(ChangeEvent arg0)
                {
                    // TODO Auto-generated method stub
                    studentlist1.get(i-1).setAbsent();            //causes index out of bounds exception @(i)
                }
            });
        }
     case 2:
         for(i=0;i<studentlist2.size();i++)
        {
            JCheckBox chbx = new JCheckBox(studentlist2.get(i).getName());
            panel.add(chbx);

            chbx.addChangeListener(new ChangeListener()
            {
                @Override
                public void stateChanged(ChangeEvent arg0)
                {
                    // TODO Auto-generated method stub
                    studentlist2.get(i-1).setAbsent();            
                }
            });
    }
  }  




Aucun commentaire:

Enregistrer un commentaire