8

How to remove the ClickHandler Event in GWT? I added addClickHandler() Event for a button and i want to remove the ClickHandler Event.I tried HandlerRegistration Method But it failed to remove the handler ,Here is a snippet :

notification.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            // TODO Auto-generated method stub
        }

    });  

I want to remove the handler with notification !

Note:
Notification is the button instance that calls the handler!
alex
  • 2,450
  • 16
  • 22
Rangesh
  • 728
  • 2
  • 12
  • 27
  • What do you mean by `it fails` and `remove the handler with notification`? – jusio Apr 26 '12 at 14:42
  • Fails refers to HandlerEvent is not removed and i have mentioned notification as button instance that calls the handler ! – Rangesh Apr 26 '12 at 14:49

2 Answers2

22

Each add...Handler method returns the HandlerRegistration interface. This interface contains the removeHandler() method. If you want to remove handlers, simple store the returned interface in a variable and call removeHandler when you want to remove the handler.

HandlerRegistration handler;

handler = button.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                // ...

            }
        });
handler.removeHandler();            
Hardik Mishra
  • 14,779
  • 9
  • 61
  • 96
1

This worked for me, I get the Handler registration when I bind the event,

closeIconHandlerRegistration = closeImg.addClickHandler( new ClickHandler()
        {
            @Override
            public void onClick( ClickEvent event )
            {
                addCloseClickHanlder();
            }
        } );

After that When I need to remove the handler...

if ( this.getCloseButtonHandlerRegistration() != null )
        {
            this.getCloseButtonHandlerRegistration().removeHandler();
            this.getCloseImg().addClickHandler( new ClickHandler()
            {

                @Override
                public void onClick( ClickEvent event )
                {
                    SaveCancelCommissionChangeEvent saveEvt = new SaveCancelCommissionChangeEvent();
                    saveEvt.setSave( false );
                    tabEventBus.fireEvent( saveEvt );
                }
            } );
        }
Maxi
  • 285
  • 6
  • 20