Frage

I'd like to be able to do in Java what the pseudo code below suggests:

for each jcheckbox in gui.panel1
  jcheckbox.setEnabled(true);
next

I don't want to enable every component; just the checkboxes.

War es hilfreich?

Lösung

  public void enableCheckboxes(){
    Component [] c = gui.panel.getComponents();
    for (Component c1 : c) {
      if (c1 instanceof JCheckBox) {
        c1.setEnabled(true);
      }
    }
  }

I'd wondered when I'd use the instanceof operator. I kept seeing it lots of places. And I was unaware of the power of getComponents(). Makes me want to tackle Reflection soon.

Thanks to Niemeyer's Learning Java, chapter 17--Using Swing Components.

If there's another way--and surely there is--I hope to see it soon.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top