-1

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.

DSlomer64
  • 4,234
  • 4
  • 53
  • 88
  • 1
    Use `instanceof` to check for `JCheckBox`. – Andrew Thompson Jan 26 '14 at 01:07
  • I was asking a question that I knew the answer to, as suggested by SO. – DSlomer64 Jan 26 '14 at 01:16
  • Whichever one of you highandmighties downvoted me, I suggest you read this [link](http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/) and scroll through it. "To be crystal clear, it is not merely OK to ask and answer your own question, it is explicitly encouraged." Frankly, though, I couldn't care less about downvotes. And if it was because the question-and-answer weren't worth asking-and-answering, show me the criteria. It is certainly not an obvious solution. – DSlomer64 Jan 26 '14 at 01:22
  • @Patterned: Please note that the checkboxes ARE, after the fact, IN an array. – DSlomer64 Jan 26 '14 at 01:32

1 Answers1

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

DSlomer64
  • 4,234
  • 4
  • 53
  • 88