How to handle multiple ClickEvents in a VerticalPanel with UiBinder?
Asked Answered
F

3

5

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?

Flopeared answered 24/2, 2011 at 13:35 Comment(0)
P
13

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
}
Palmira answered 24/2, 2011 at 13:44 Comment(2)
Thanks a lot Riley. I always thought the handleClick() from the tutorials was a naming convention, but I guess it isn'tFlopeared
My biggest complaint about GWT is that so much is generated on top of the Java code - it can be really hard to tell how things are connected, and even harder to tell why things aren't connected ;)Palmira
C
29

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

@UiHandler(value={"clearButton_1", "clearButton_2"})
void handleClickForLabel1(ClickEvent event) {
     //do stuff
}
Cyaneous answered 27/10, 2011 at 14:43 Comment(2)
Use if(event.getSource().equals(clearButton_1)) Window.alert("Button1"); to get the clicked widget.Indisposed
So, for instance, I have to deal with the exactly same way from two different UiField, so thanks for the exampleSalol
P
13

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
}
Palmira answered 24/2, 2011 at 13:44 Comment(2)
Thanks a lot Riley. I always thought the handleClick() from the tutorials was a naming convention, but I guess it isn'tFlopeared
My biggest complaint about GWT is that so much is generated on top of the Java code - it can be really hard to tell how things are connected, and even harder to tell why things aren't connected ;)Palmira
S
0

I ran in to this situation and found that event.getSource() only gives an instance of the source object not its name. I had to cast it and get its title to identify the source object. In my case I'm using MaterialImage and setting its title in the UiBinder.

Example: UiBinder code

<m:MaterialImage url="images/icons/simpleLine.svg" ui:field="simpleLine" title="simpleLine" />
<m:MaterialImage url="images/icons/smallDashBigGap.svg"ui:field="smallDashBigGap" title="smallDashBigGap" />

In Java

Object object = event.getSource();
if (object instanceof MaterialImage) {
    MaterialImage image = (MaterialImage) object;
    String type = image.getTitle();
    if (type.equals("simpleLine")) {
        ...
    }

I wish there is a better way but that's all I could work with.

Spellbind answered 9/2, 2016 at 18:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.