简体   繁体   中英

How to get files from sub-directory into ArrayList using java?

I have a package, which contains a lot of java swing components all of them extend a range of different JSwing components. In a separate file I want to access all of these components and put them into an ArrayList ?

I can do it in any way i want but i do not see how i could get it from looping through every file and I also do not know how to do it if I import it into an ArrayList dynamically.

For example:

-MyComponentsPackage
  -Component1 (This extends JLabel)
  -Component2 (This extends JButton)
  -...

in another file I need to do:

private List<JComponent> = new ArrayList<JComponent>();

I want to be able to loop through every object and add them to a JPanel:

for (Component componentInPackage : allComponentsInPackage) {
    myPanel.add(componentInPackage);
}

I'd suggest checking out this post: Can you find all classes in a package using reflection?

It provides a solution that you could use, but also goes into an explanation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM