title: Java - Select All GUI Components of Type
body: <p>I'm trying to code my program so that all the components on my JFrame have a consistent colour scheme. To be able to do this, I wanted to use Java's for-each loop to affect each component of the same type. I have tried it like this to colour the JPanels, but it has had no affect.</p>
  
  <pre><code>public void colourGUI()
  {
      for (Component comp : this.getComponents())
      {
          if (comp instanceof JPanel)
          {
              ((JPanel) comp).setOpaque(true);
              comp.setBackground(Color.gray);
          }
      }
  }
  </code></pre>
  
  <p>I would have imagined this would work, but when I loaded the application it hadn't taken affect.
  I'm reasonably new to Java, and I have previously used C# for programs like this where I would have used C#'s <code>OfType&lt;&gt;</code> method. I was hoping Java would have some kind of equivalent but I haven't found anything yet.</p>
  
  <p>Would anyone be able to point me in the right direction to accomplish what I'm looking to do? I imagine I'm missing something really obvious!</p>
  
  <p>Thanks,
  Mark</p>