Iterate over components Swing recursively is strange

Asked

Viewed 43 times

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

  • 1

    Please provide a [mcve] so that it is possible to test and execute the code and thus simulate the problem.

  • 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

  • 1

    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.

  • 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.

  • 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

  • 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 :(

  • 2

    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

  • 1

    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

Show 3 more comments

1 answer

1


I found out :

e.getComponent()

returns the container in which it will be added, not the component that will be added: changed to

e.getChild()

And now it works :o

Btw, I have many components on the same screen :s

  • 2

    Exactly what I had commented.

  • https://answall.com/questions/283563/iterar-sobre-árvores-não-recursivamente#comment578820_283563

  • Yep, had misunderstood your comment :

Browser other questions tagged

You are not signed in. Login or sign up in order to post.