How to view the status of check boxes in the Java GUI?
I have about 200 check boxes in the Java GUI Now I want to list all the checkboxes that the user has checked
I can do this in this way:
jCheck@R_424_2419@1.isSelected();
But I don't want to write this line for 200 check boxes There is no way to do this through the for loop
The names of all check boxes are jCheck@R_424_2419 @1, jCheck@R_424_2419 @2, jCheck@R_424_2419 @3, jCheck@R_424_2419 @4 … jCheck@R_424_2419 @200
Solution
You really should put them in an array or collection so that you can traverse them For example
List<JCheck@R_424_2419@> allCheck@R_424_2419@es = new ArrayList<JCheck@R_424_2419@>() allCheck@R_424_2419@es.add(new JCheck@R_424_2419@());
wait
If you declare all of these check boxes as members, there is no reason to put them in the list
In the meantime, you can use tricky casts in the for loop (if all check boxes are on the same panel)
boolean allSelected = true; for(Component component : myPanel.getComponents()) { if(component instanceof JCheck@R_424_2419@) { allSelected &= ((JCheck@R_424_2419@)component).isSelected(); } }
Before continuing, I recommend that you read about Java arrays and collections
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html
http://java.sun.com/docs/books/tutorial/collections/index.html