• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java GUI: using checkboxes  RSS feed

 
Konrad Cetes
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

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!

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain more about what the method is supposed to do. There is something weird about seeing you count 4 and then reduce the count.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!