问题
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?
回答1:
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);
}
}
回答2:
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);
}
}
来源:https://stackoverflow.com/questions/6485634/get-all-swing-components-in-a-container