• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Java GUI: using checkboxes

 
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: 70657
288
  • 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: 732
10
  • 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.
 
Bartender
Posts: 4103
156
  • 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: 70657
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • 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
 
I wasn't selected to go to mars. This tiny ad got in ahead of me:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic