Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

ComponentAttach and Detach Events

Nicole Monit
1 decade ago Sep 21, 2011 4:38pm
Charles Anthony
1 decade ago Sep 22, 2011 7:06am

Hi,

Currently, there is no simple way to do this in core Vaadin.

In my project, I have created some iterators that - given an arbitrary component - will walk the tree of components beneath it.
Oh - I've just remembered, I put this code on BitBucket here. Feel free to use and abuse as you like; FWIW, I'm not planning to maintain the project (I just pulled the stuff out of my closed source project).

If you wanted to have certain components notified on attach/detach, I'd implement a marker interface, implement ComponentAttachListener on the root component, and do something like this :

Iterable<? extends Component> componentTree = ComponentIterables.componentTreeBreadthFirst(this);

    for (Component component : componentTree) {
      if (component instanceof YourMarker) {
        ((YourMarker) thing).componentAdded();        
      }
    }

You could also have a self propogating ComponentAttach/COmponentDetach listener, which adds/removes itself from all ComponentContainer's : just add it once at the Window/Root level, and you can be notified as any object gets added or removed from the component tree.

In short, it's possible - but you need to do a bit of work yourself.

HTH,

Cheers,

Charles

Last updated on Sep, 22nd 2011