I have this code to get all the elements I need and do some processing. The problem is I need to specify every panel I have to get the elements inside it.
fo
If you want to find all components of a given type, then you can use this recursive method!
public static List findComponents(
final Container container,
final Class componentType
) {
return Stream.concat(
Arrays.stream(container.getComponents())
.filter(componentType::isInstance)
.map(componentType::cast),
Arrays.stream(container.getComponents())
.filter(Container.class::isInstance)
.map(Container.class::cast)
.flatMap(c -> findComponents(c, componentType).stream())
).collect(Collectors.toList());
}
and it can be used like this:
// list all components:
findComponents(container, JComponent.class).stream().forEach(System.out::println);
// list components that are buttons
findComponents(container, JButton.class).stream().forEach(System.out::println);