Search code examples
javaswingactionlistenerjcheckbox

JCheckBox in Java to take of tick in all checkbox pressing the button


I want to take off the tick from all checkbox what I have in GUI, when I pressed on button - dynamically. Is it possible?

JButton clean = new JButton("Clean");
clean.addActionListener(new MyCleanListener());
buttonBox.add(clean);

public class MyCleanListener implements ActionListener{
  public void actionPerformed(ActionEvent a){
     if(jCheckBox.isSelected())
        c.setSelected(false);
  }
}

Thank everyone for your help.

public class MyCleanListener implements ActionListener{
      public void actionPerformed(ActionEvent a){
        for (int i = 0; i < 256; i++){
          c = checkboxList.get(i);
          c.setSelected(false);
          }
        }
    }

here my decision.


Solution

  •     panel.add(box1);
        panel.add(box2);
        panel.add(clean);
    
        clean.addActionListener(new ActionListener() {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                Component[] components = panel.getComponents();
                for (Component component : components) {
                    if (component instanceof  JCheckBox) {
                        JCheckBox checkBox = (JCheckBox) component;
                        if(checkBox.isSelected()) {                     
                        checkBox.setSelected(false);
                        }
                    }
                }
            }
        });
    

    Get all JCheckBox components from the panel and remove selection.