Iterate over components Swing recursively is strange [closed]

0

To add Listeners to all the components of my Frame I'm iterating over 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 stack overflow problem

    
asked by anonymous 16.03.2018 / 19:56

1 answer

1

DEscobri: s

e.getComponent()

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

e.getChild()

And now it works:

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

    
16.03.2018 / 21:05