How to handle multiple ClickEvents in a VerticalPanel using 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 now I want to add ClickHandlers to all three shortcuts, for example:

 @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 am getting an error because I have 3 methods with the same name. Is there any way around this other than creating custom widgets and adding them to the VerticalPanel?

+4
source share
3 answers

Just call them different things. The important part that helps the GWT recognize which event you want to handle is ClickEvent , but the method name does not 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 } 
+12
source

There is also the option to use one annotation for multiple widgets.

 @UiHandler(value={"clearButton_1", "clearButton_2"}) void handleClickForLabel1(ClickEvent event) { //do stuff } 
+27
source

I came across this situation and found that event.getSource () provides an instance of the source object, not its name. I had to drop it and get its name to identify the original object. In my case, I use MaterialImage and set its title to 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 want a better way, but all that I could work with.

0
source

Source: https://habr.com/ru/post/1341140/


All Articles