Iulian Iulian - 24 days ago 5
Java Question

Clear check boxes

I want to make a button that unchecks a set of check boxes.

Here is some of my code:

for (int i = 0; i < 256; i++) {
JCheckBox c = new JCheckBox();
c.setSelected(false);
checkboxList.add(c);
mainPanel.add(c);
} // creating checkboxes

// I created the button
JButton clearCheck = new JButton("Clear Check");
clearCheck.addActionListener(new MyClearCheckListener());
buttonBox.add(clearCheck);


I don't know what action to implement for this. I've added a print screen on how the app looks.


Answer

I have created an example in which we get all the components of the content pane with the getComponents() method and find out all JCheckBoxes with the instanceof operator. The setSelected() method selects/deselects a JCheckBox.

The following example has two buttons: for checking and unchecking all check boxes:

package com.zetcode;

import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import net.miginfocom.swing.MigLayout;

public class UncheckingEx extends JFrame {

    public UncheckingEx() {

        initUI();
    }

    private void initUI() {

        setLayout(new MigLayout("nogrid"));

        JCheckBox box1 = new JCheckBox();
        JCheckBox box2 = new JCheckBox();
        JCheckBox box3 = new JCheckBox();
        JCheckBox box4 = new JCheckBox();
        JCheckBox box5 = new JCheckBox();
        JCheckBox box6 = new JCheckBox();
        JCheckBox box7 = new JCheckBox();
        JCheckBox box8 = new JCheckBox();
        JCheckBox box9 = new JCheckBox();
        JCheckBox box10 = new JCheckBox();
        JCheckBox box11 = new JCheckBox();
        JCheckBox box12 = new JCheckBox();
        JCheckBox box13 = new JCheckBox();
        JCheckBox box14 = new JCheckBox();
        JCheckBox box15 = new JCheckBox();
        JCheckBox box16 = new JCheckBox();

        JButton btn1 = new JButton("Check all");
        btn1.addActionListener(new CheckAllAction());

        JButton btn2 = new JButton("Uncheck all");            
        btn2.addActionListener(new UnCheckAllAction());      

        add(box1);
        add(box2);
        add(box3);
        add(box4, "wrap");
        add(box5);
        add(box6);
        add(box7);
        add(box8, "wrap");
        add(box9);
        add(box10);
        add(box11);
        add(box12, "wrap");
        add(box13);
        add(box14);
        add(box15);
        add(box16, "wrap");

        add(btn1, "sgx");
        add(btn2, "sgx");

        pack();

        setTitle("JTextPane");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

     private class CheckAllAction extends AbstractAction {

        @Override
        public void actionPerformed(ActionEvent e) {

            Component[] components = (Component[]) 
                    getContentPane().getComponents();

            for (Component comp : components) {

                if (comp instanceof JCheckBox) {
                    JCheckBox box = (JCheckBox) comp;
                    box.setSelected(true);
                }
            }            
        }
     }

     private class UnCheckAllAction extends AbstractAction {

        @Override
        public void actionPerformed(ActionEvent e) {

            Component[] components = (Component[]) 
                    getContentPane().getComponents();

            for (Component comp : components) {

                if (comp instanceof JCheckBox) {
                    JCheckBox box = (JCheckBox) comp;
                    box.setSelected(false);
                }
            }            
        }
     }     

    public static void main(String[] args) {

        EventQueue.invokeLater(() -> {
            UncheckingEx ex = new UncheckingEx();
            ex.setVisible(true);
        });
    }
}

This is a screenshot:

Example screenshot

Comments