سؤال

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.

هل كانت مفيدة؟

المحلول

    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.

نصائح أخرى

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top