Using @UiHandler for click event on custom widget

291 views
Skip to first unread message

Jonas

unread,
Mar 17, 2012, 4:58:50 PM3/17/12
to google-we...@googlegroups.com
I have created a custom widget which is basically a HTMLPanel that extends Composite and is styled as a button.
I'm using the widget with UiBinder, but I want to use the @UiHandler annotation inside the view where the button
is located and catch its click event.

How can I do this? I have tried doing something like this but it doesn't work:

@UiField
HTMLPanel mainContainer;

@UiConstructor
public CustomButton(String text)
{       
    initWidget(binder.createAndBindUi(this));
    setText(text);
   
    mainContainer.addDomHandler(new ClickHandler()
    {
        @Override
        public void onClick(ClickEvent event)
        {
            Window.alert("Clicky");               
            fireEvent(event);
        }           
    }, ClickEvent.getType());
}

In my view:

@UiHandler("customButton")
public void customButton(ClickEvent event)
{
    Window.alert("Clicky!");
}

I get error:

[ERROR] Field 'customButton' does not have an 'addClickHandler' method associated.

Jens

unread,
Mar 17, 2012, 5:27:57 PM3/17/12
to google-we...@googlegroups.com
Just implement the interface HasClickHandlers in your custom button so you have an addClickHandler() method.

public CustomButton extends Composite implements HasClickHandlers {

  public HandlerRegistration addClickHandler(ClickHandler handler) {
    return addDomHandler(handler, ClickEvent.getType());
  }

}

-- J.
Reply all
Reply to author
Forward
0 new messages