List<JCheckBox> checkboxes = new ArrayList<JCheckBox>(); for( Component comp : panel.getComponents() ) { if( comp instanceof JCheckBox) checkboxes.add( (JCheckBox)comp ); }
All JCheckBox instances are assumed to be direct children of the container panel. If not, you will need to recursively visit all panel containers using the same logic. Now, although you can do this, itβs usually best to keep these links when you create them in the list. Then you can easily iterate over all the checkboxes without having to do this code above. If you have built-in components, it is better to ask the embedded component to perform any operation that you need on the flags that it owns (as opposed to pulling them out of the component through the getter, so that you can somehow ruin them).
source share