Bee
Bee

Reputation: 12512

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?

Upvotes: 3

Views: 5327

Answers (2)

Nathan Moos
Nathan Moos

Reputation: 3643

You can call getComponents then test to see if it is an instance of JComponent. A method would be like:

ArrayList jcomponents = new ArrayList();
for (Component c : container.getComponents())
{
      if (c instanceof JComponent)
      {
            jcomponents.add(c);
      }
 }

Upvotes: 1

Jeffrey
Jeffrey

Reputation: 44808

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);
     }
}

Upvotes: 2

Related Questions