Get all swing components in a container

南楼画角 提交于 2019-12-22 11:21:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!