0
To add Listeners to all components of my Frame I’m iterating on it recursively:
private void adicionarListeners(Component componente)
{
Stack<Component> stack = new Stack<Component>();
// evita adicionar o listener mais de uma vez no mesmo componente
if (!componentesRegistrados.contains(componente)) {
componente.addMouseListener(listenerMouse);
// LOGGER.log(Level.INFO, "Adicionou listener em {0}", componente.getName());
System.out.println("Adicionou listner em "+componente.getName());
componentesRegistrados.add(componente);
}
if (componente instanceof Container) {
Container container = (Container) componente;
int filhos = container.getComponentCount();
for (int i = 0; i < filhos; i++) {
adicionarListeners(container.getComponent(i));
}
container.addContainerListener(new ContainerAdapter()
{
@Override
public void componentAdded(ContainerEvent e)
{
adicionarListeners(e.getComponent());
}
});
}
}
I’m having battery pop problem
Please provide a [mcve] so that it is possible to test and execute the code and thus simulate the problem.
– user28595
So, there is no minimal example, Overflow only happens because it has a lot (many components), but the real question is: how to go through Swing components (or any tree) in a non-recursive way
– Alisson Steffens
Without a [mcve] It’s hard to help, because there’s no way of knowing what kind of components are on your screen that might conflict with that method. Suddenly his problem is something silly that can be solved easily. But you need to collaborate and provide a [mcve] for that reason.
– user28595
And it’s rare for questions involving swing to be answered without an executable example, especially with this level of complexity. I suggest you try to create a code within the templates of the link I posted, so it will make it easier for you to get answer.
– user28595
Could it not be hypothetically a case in which it is not a tree but a recurring graph? To burst stack with a depth less than 50 of depth I believe that there is not
– Jefferson Quesado
I’ll see if I can reproduce in another example @Articuno; btw Jefferson, I’m putting each component that registers in a "componentsRegistered" Hashmap, so recidivism should not happen :(
– Alisson Steffens
Another problem with this code is that if the goal is to add Listener to all the child components, there should be no need to call the method itself within the Listener again. What should be done is to check if the current component within the loop is of the Container type, if it is, you call the method itself recursively, if it is not, apply the Systener
– user28595
Do you have 25,000 components on your screen? So the problem is not only with Java, but also with the structure of your application
– user28595