• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java GUI: using checkboxes

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic