Search code examples
javaswingawtjcomponent

Get all swing components in a container


I think we can use jScrollPane.getComponents() to get awt components of a jscrollpane. My question is: is there a way to get swing components of a container some how?


Solution

  • All Swing components extend JComponent.

    Component[] comps = jScrollPane.getComponents();
    ArrayList<JComponent> swingComps = new ArrayList<JComponent>();
    
    for(Component comp : comps) {
         if(comp instanceof JComponent) {
              swingComps.add((JComponent) comp);
         }
    }