Search code examples
eventsgwtevent-handlinguibinder

How to handle multiple ClickEvents in a VerticalPanel with UiBinder?


Assuming the following *.ui.xml file:

<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
        xmlns:g='urn:import:com.google.gwt.user.client.ui'>
<g:VerticalPanel>
    <g:Label ui:field="Label1"></g:Label>
    <g:Label ui:field="Label2"></g:Label>
    <g:Label ui:field="Label3"></g:Label>
</g:VerticalPanel>

If I now want to add ClickHandlers to all three Labels like this:

@UiHandler("Label1")
void handleClick(ClickEvent event) {
    //do stuff
}
@UiHandler("Label2")
void handleClick(ClickEvent event) {
    //do stuff
}
@UiHandler("Label3")
void handleClick(ClickEvent event) {
    //do stuff
}

I get an error, because I have 3 methods with the same name. Is there a way around this, other than creating custom widgets and add those to the VerticalPanel?


Solution

  • Just name them different things. The important part that helps GWT recognize what kind of event you want to handle is the ClickEvent, but the method name doesn't matter.

    @UiHandler("Label1")
    void handleClickForLabel1(ClickEvent event) {
        //do stuff
    }
    
    @UiHandler("Label2")
    void handleClickForLabel2(ClickEvent event) {
        //do stuff
    }
    
    @UiHandler("Label3")
    void whoaSomeoneClickedLabel3(ClickEvent event) {
        //do stuff
    }