I have 30 check boxes in a JFrame that i have. I am trying to write the code so that when one check box is selected, the ones before are also selected.
I.e.(if box 4 is selected then boxes 1, 2, and 3 will also be selected.)
I have a counter, and i tried multiple ways but it seems to not be working.
Any suggestions would be great!
1. Why is the variable dayCount declared as static?
2. What is the purpose of that variable? (see also Campbell Ritchie's post regarding it)
3. Why do you have a method called day4ActionPerformed? Where is that method called? Note that it is NOT called automatically when the box is checked, since it is not a method that satisfies the ActionListener interface.
4. Consider placing all of the JCheckBoxes in an array or ArrayList; that will make it easier to set other checkboxes when one of them is set.
Also easy is to extend JCheckBox, so that it takes a sequential number 'id' going from 0 upward. In the ActionListener you can get this id by ((JCheckBox) actionEvent.getSource()).getId, and in combination with Fred's array it is trivial to set all the foregoing JCheckBoxes.
There are three kinds of actuaries: those who can count, and those who can't.
Also if the JComboBoxes are in an ArrayList, the ArrayList's indexOf() method returns the index of the selected item (available from getSource()) allowing you to access the JComboBoxes before the selected one.
There is no pressing need to maintain an array/list of the checkboxes. The Container (which JPanel subclasses) has convenient methods to get all the child components. These can be used to confirm JCheckBox instance and manipulate the selection state as required