2

I'm developing a small aplication with java and swing and I'm facing a problem that might make me do a part of it from scratch.

In one of the panels I have around 20 checkboxes. So far the variable names are jcheckbox1, jcheckbox2, etc. I need to iterate through all of them and retrieve the text of the selected ones. The problem comes with the fact that I have used NetBeans to develop my GUI by dragging and dropping the elements as I needed them, so I don't have my checkboxes aggregated in a list or an array. Given this, is there any possibility to select all checkboxes and iterate through them to retrive the text value of the ones checked?

If there isn't, how should I declare them in a way that I can iterate easily through them? I believe an array of checkboxes might not be very hard to do by itself, but the trouble seems to come from placing them all in the correct positions. I'm using gridbag with two columns, if that helps.

Thanks!

Paul
  • 19,704
  • 14
  • 78
  • 96
Hugo Torres
  • 308
  • 4
  • 13
  • 1
    `I have used NetBeans to develop my GUI by dragging and dropping the elements as I needed them` - don't use the IDE to create the form. As you can see it is a problem for this question and also the code is not portable if you ever need to move to a different IDE. Instead code the form yourself and use an appropriate layout manager to get your desired layout. The code will be easier to maintain and use. For example start with the demo code from [How to Use GridLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html). – camickr Dec 09 '16 at 15:20

4 Answers4

3

Given a Container, you can find all the child components, then check if they are JCheckBox, and then if they are checked.

The following example method is inspired from How to get all elements inside a JFrame? , and adapted to your needs :

public static List<String> manageCheckedCheckboxes(final Container c) {
    Component[] comps = c.getComponents();
    List<String> checkedTexts = new ArrayList<String>();

    for (Component comp : comps) {

        if (comp instanceof JCheckBox) {
            JCheckBox box = (JCheckBox) comp;
            if (box.isSelected()) {

                String text = box.getText();
                checkedTexts.add(text);
            }
        }
    }

    return checkedTexts;

}
Community
  • 1
  • 1
Arnaud
  • 17,229
  • 3
  • 31
  • 44
1

You can iterate through the components in your container and see if they can be cast to checkboxes.

List<JCheckBox> checkboxes = Arrays.stream(container.getComponents())
    .filter(c -> c instanceof JCheckBox)
    .map(JCheckBox.class::cast)
    .collect(Collectors.toList());

List<String> selectedText = checkboxes.stream()
    .filter(JCheckBox::isSelected)
    .collect(Collectors.toList());

For fun I made a generic recursive version for finding any component you may need:

public static <T extends JComponent> List<T> findComponents(
    final Container container,
    final Class<T> componentType
) {
    return Stream.concat(
        Arrays.stream(container.getComponents())
            .filter(componentType::isInstance)
            .map(componentType::cast),
        Arrays.stream(container.getComponents())
            .filter(Container.class::isInstance)
            .map(Container.class::cast)
            .flatMap(c -> findComponents(c, componentType).stream())
    ).collect(Collectors.toList());
}
flakes
  • 21,558
  • 8
  • 41
  • 88
0

Netbeans generate code for drag & drop components so you can add them manually to list or other collection and then iterate over it.

piterpti
  • 11
  • 3
0

Netbeans still allows to write custom code in the form design, all you have to do is go to source, create an array of checkboxes.

Create an array, on the bottom of the page, where can find other variables: Create an array

Initialize array as:

Initialize array as

Later you can simply iterate over the array

for(JCkeckBox cb: ckeckBoxes){
    //
}