dynamic listeners

Hi,

I have a component that creates buttons and may have the listeners changed dynamically. How can I remove all existing listeners on a button?

Thanks,

Mark

Did I miss something in the docs or is this a rare case? Any help much appreciated!

bump

i don’t think a Button.removealllisteners() type api exists but you can keep track of listeners in a List as you add them and write your own removeall() method that iterates through this list and calls Button.removeListener(…)

maybe you can open a support ticket if one already doesn’t exist if you want it to be in the core api.

OK, thanks.

Although this is two years old … in Vaadin 7 there is a method removeAllListeners() on the EventRouter class.
In Vaadin 6 you can remove all listeners for a specific event type like this:


removeListener(ClickListener.class, myButton);

Bye,
Dirk

Hi,

i think removeListener(ClickListener.class, myButton); is broken…

it has no effect!

Hi,
are you talking about removeListener method of EventRouter?
EventRouter.removeListener takes the event class as first argument not the listener class

Hi,

    removeListener(ClickEvent.class, button1);
    removeListener(ClickListener.class, button1);

well you mean ClickEvent would be the correct way to remove the listeners?
I tried it and it is also not working. The click events are always fired…

Button button1 = new Button();
button1.addClickListener(new ClickListener() {
      @Override
      public void buttonClick(ClickEvent event) {
        // do some stuff
      }
});

Am i missing something?

You must pass the listener instance to the removeListener method if you want to remove it, not the button.

ClickListener listener = new ClickListener() {
      @Override
      public void buttonClick(ClickEvent event) {
        // do some stuff
      }
};
Button button1 = new Button();
button1.addClickListener(listener);

// ....
// Some place in your code where you want to remove the listener
button1.removeClickListener(listener);

If you want to remove all listeners for a given event type you can do something like this

button1.getListeners(ClickEvent.class)
     .stream()
     .forEach( listener -> button1.removeListener(ClickEvent.class, listener));

Hi,

button1.getListeners(ClickEvent.class) .stream() .forEach( listener -> button1.removeListener(ClickEvent.class, listener)); is the way… thx

By the way, you don’t need to get a stream in this case (forgot to delete the statement :slight_smile: )

button1.getListeners(ClickEvent.class)
    .forEach( listener -> button1.removeListener(ClickEvent.class, listener));

Cheers
Marco