• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Java GUI: using checkboxes  RSS feed

 
Greenhorn
Posts: 28
  • 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!

 
Marshal
Posts: 59762
188
  • 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.
 
Bartender
Posts: 619
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.
 
Master Rancher
Posts: 2758
93
  • 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.
 
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: 59762
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
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!