0

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.

Alex K
  • 8,269
  • 9
  • 39
  • 57
Stas
  • 3
  • 4
  • `I want to take off the tick` --> do you mean to block all mouse and key events to JCheckBox – mKorbel Apr 04 '14 at 08:21
  • My approach to similar situations (may be not the best, but good enough) - group all your checkboxes that you want to process together in JPanel or in some other way, then just iterate through the members of this group. BTW: you can `setSelected(false);` irrespective of it's current state, there is not much overhead in it. – Germann Arlington Apr 04 '14 at 08:28

2 Answers2

0

Memorize all your checkboxes that you need unticked in some list or other data structure, then you can iterate through it in your clean listener's action performed method and untick them all...

0
    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.

Laxman
  • 2,643
  • 2
  • 25
  • 32