Documentation for @UiHandler

匆匆过客 提交于 2019-12-20 10:26:35

问题


I started to look into using GWT in combination with UiBuilder. I'm a bit puzzled about how you can use the @UiHandler(..) directive to make simple event handle code as written down in the GWT documentation:

@UiHandler("button")
void handleClick(ClickEvent e) {
  Window.alert("Hello, AJAX");
}

In this case the method handleClick is used. How do you know for each GWT widget what methods can be created with @UiHandler? For some you can also create a doClose() method.

But what can you use with, for instance, a ListBox to get an event an item is selected? Where in the documentation can I see this?


回答1:


The parameter you pass to the @UiHandler annotation is the name of the appropriate field you want to assign that *Handler. So, in this case you are assigning a ClickHandler to a Button button (actually, we just know the field's name).

As for how this exactly works - it's part of GWT magic :) My guess is that, just like any other UiBinder related code (I think there was a presentation on Google IO, that showed the code that UiBinder generates), at compilation time the compiler figures out what goes where. In this example: we have a Button button, and we have a @UiHandler annotated method that has a ClickEvent parameter -> that must mean it's a ClickHandler (notice that the method's name doesn't matter). So let's add some code at compile time (in the constructor, probably) that adds that handler to the button. If you are interested in a more comprehensive answer - check out the source :D

But what can you use with, for instance, a ListBox to get an event an item is selected? Where in the documentation can I see this?

In the GWT API reference. In this case, you are probably looking for ListBox.addChangeHandler. But you usually won't find @UiHandler related code there - that's because it would be redundant - you always construct the @UiHandler methods the same way:

  1. You check the *Handler that you want to add, say ChangeHandler
  2. It has a void onChange(ChangeEvent event) - so, your method needs a ChangeEvent parameter and should look like this:

    @UiHandler("listBox")
    void whateverName(ChangeEvent event) {
        // ...
    }
    



回答2:


Probably your problem is in your onModuleLoad method:

public void onModuleLoad() 
{       
    HelloWorld helloWorld = new HelloWorld("BOTAO"); 

    // Using this way @UiHandler will not work
    //Document.get().getBody().appendChild(helloWorld.getElement()); 

    // correct way
    RootPanel.get().add(helloWorld);  
}


来源:https://stackoverflow.com/questions/3440763/documentation-for-uihandler

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!