简体   繁体   中英

Iterate through all checkboxes in Swing

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!

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;

}

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());
}

Netbeans为拖放组件生成代码,因此您可以将它们手动添加到列表或其他集合中,然后对其进行迭代。

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: 创建一个数组

Initialize array as:

初始化为

Later you can simply iterate over the array

for(JCkeckBox cb: ckeckBoxes){
    //
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM