Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 861
Chris Boesing Avatar asked Feb 24 '11 13:02

Chris Boesing


2 Answers

There is also an option to use one annotation for multiple Widgets

@UiHandler(value={"clearButton_1", "clearButton_2"})
void handleClickForLabel1(ClickEvent event) {
     //do stuff
}
like image 183
vladaman Avatar answered Nov 09 '22 10:11

vladaman


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
}
like image 40
Riley Lark Avatar answered Nov 09 '22 12:11

Riley Lark