How to get all elements inside a JFrame?
Asked Answered
H

4

21

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.

for (Component c : panCrawling.getComponents()) {
    //processing
}
for (Component c : panFile.getComponents()) {
    //processing
}
for (Component c : panThread.getComponents()) {
    //processing
}
for (Component c : panLog.getComponents()) {
    //processing
}
//continue to all panels

I want to do something like this and get all the elements without need specefy all the panels names. How I do this. The code below don't get all the elements.

for (Component c : this.getComponents()) {
    //processing
}
Hubert answered 27/6, 2011 at 16:13 Comment(0)
J
42

You can write a recursive method and recurse on every container:

This site provides some sample code:

public static List<Component> getAllComponents(final Container c) {
    Component[] comps = c.getComponents();
    List<Component> compList = new ArrayList<Component>();
    for (Component comp : comps) {
        compList.add(comp);
        if (comp instanceof Container)
            compList.addAll(getAllComponents((Container) comp));
    }
    return compList;
}

If you only want the components of the immediate sub-components, you could limit the recursion depth to 2.

Joleen answered 27/6, 2011 at 16:16 Comment(2)
I put a check in the beginning to verify if is a JFrame. The other answer was essencial too understand how to get components inside a JFrame.Hubert
Very clean. Thank youEthnarch
T
15

Look at the doc for JFrame. Everything you put in a JFrame is actually put in a root pane contained in the frame.

for (Component c : this.getRootPane().getComponents())    
Triplicity answered 27/6, 2011 at 16:21 Comment(1)
Thanks, I was not getting the root pane, I do not known this.Hubert
W
0
            for (Component c : mainPanel.getComponents()) {
                for (Component sc : ((JPanel) c).getComponents()) {
                    if (sc instanceof JTextField) {
                        //process
                    }
                }
            }

in my code, im just getting all instances of jtextfield. u can use same logic. this is just example of getting all sub-components from components you have taken. Hope it will help u out.

Wylde answered 30/11, 2016 at 12:11 Comment(0)
N
0

If you want to find all components of a given type, then you can use this recursive method!

public static <T extends JComponent> List<T> findComponents(
    final Container container,
    final Class<T> 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);
Norford answered 9/12, 2016 at 16:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.