13

How can I get all the components of a panel in Java Swing?

Is there any method like foreach in C# to deal all the child components of a JPanel?

Roman C
  • 49,761
  • 33
  • 66
  • 176

3 Answers3

19

You may use the method getComponents:

Component[] components = jpanel.getComponents();
Howard
  • 38,639
  • 9
  • 64
  • 83
  • This will get the child components, but not those components' children. For example, if your panel has containers such as CollapsiblePanel, the set `components` will contain a reference to that CollapsiblePanel, but not its child components. – Jonathan E. Landrum Oct 21 '20 at 15:25
1

if you have more than one JPanel and you want to get all components Name try this:

public void getAllComponetsNameInJPanels(JPanel... panels) {
    Component[] components;
    String componentName;
    for (JPanel panel : panels) {
        components = panel.getComponents();            
        for (Component compo : components) {
            componentName = compo.getClass().getName();
            System.out.println(compo.getClass().getName().substring(componentName.indexOf("swing.") + "swing.".length(), componentName.length()));
        }
        System.out.println("=====================");
    }
}
0

getComponents() method will return all child components of a given components as an array.

If you want to get all components including child components of child components you can write a recursive function and consolidate everything into one List.

Recursion, in programming, is a technique of solving problems by creating a method that calls itself until the desired result is achieved.

Below is an example that will allow you to get a component by name, regardless how deep it is within the structure:

// Container is imported from java.awt
public static Container getChildComponentByName(Container parentComponent, String childName) {

    // Define data structures that will hold components
    Map<String, Container> allComponentsMap = new HashMap();
    List<Container> allComponents = new ArrayList<>();

    // Iterating through the components structure and adding it to the List using our recursive function
    addAllChildComponentsToList(allComponents, parentComponent);

    // Iterating through the List and adding them to a HashMap keyed with their name
    for (Container c : allComponents) {
        allComponentsMap.put(c.getName(), c);
    }

    // Returning a component with the given name
    if (allComponentsMap.containsKey(childName)) {
        return allComponentsMap.get(childName);
    } else {
        System.out.println("ERROR: No match found when looking for GUI child components.");
        return null;
    }
}

private static void addAllChildComponentsToList(List<Container> componentArr, Container parentComponent) {

    // Making a list with all child components
    List<Container> childComponentsArr = Arrays.stream(parentComponent.getComponents()).map(c -> (Container) c).collect(Collectors.toList());

    if (childComponentsArr.size() > 0) {

        for (Container c : childComponentsArr) {

            // Adding a child component to the passed List
            componentArr.add(c);

            // Repeating the process if child has its own child components
            if (c.getComponents().length > 0) {
                addAllChildComponentsToList(componentArr, c);
            }
        }
    } else {
        return;
    }

}
Vahe Aslanyan
  • 76
  • 1
  • 4