문제

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.

도움이 되었습니까?

해결책

  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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top