If you want to have all elements from a Container (like JPanel or JTabbedPane, etc...) you need to collect them manually, because the getComponents() is only reading the direct children of the Container.
You need to add the following function to the Sample class:
public List<Component> getAllComponents(Container container) {
Component[] components = container.getComponents();
List <Component> result = new ArrayList<Component>();
for (Component component : components) {
result.add(component);
if (component instanceof Container) {
result.addAll(getAllComponents((Container) component));
}
}
return result;
}
You need to replace the following code:
Component[] component =((ExampleTab1)obj).getContentPanel().getComponents();
with this code: List<Component> components = getAllComponents(((ExampleTab1)obj).getContentPanel());
and don't forget the null check for the getName()
function, if you have components without name.