Search code examples
javagwtuibinder

GWT @UiHandler dont work


I'm using GWT UiBinder... but I cant use @UiHandler because it not work.

xml

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
    xmlns:g="urn:import:com.google.gwt.user.client.ui">

        <g:Button ui:field="btn" text="btn"></g:Button>

</ui:UiBinder>

Java

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

public class HowToHelp extends Composite {

    interface HowToHelpUiBinder extends UiBinder<Widget, HowToHelp> {
    }

    private static HowToHelpUiBinder uiBinder = GWT
            .create(HowToHelpUiBinder.class);

    @UiField
    Button btn;

    public HowToHelp() {
        initWidget(uiBinder.createAndBindUi(this));
    }

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

}

the Window.alert("Hello, AJAX"); never is called. I did exactly what was passed on the official GWT: http://www.gwtproject.org/doc/latest/DevGuideUiBinder.html

There is a complect project with some error zip


Solution

  • From the linked zip, code missing from the question:

    package source.client;
    
    import com.google.gwt.core.client.EntryPoint;
    import com.google.gwt.user.client.ui.RootPanel;
    
    public class UiHandlerTest implements EntryPoint {
    
        @Override
        public void onModuleLoad() {
            RootPanel.getBodyElement().appendChild(new TheHandlerTest("My Button").getElement());
    
        }
    
    }
    

    The problem is that you are adding a widget to another widget without actually doing the add part of it ;). Instead, you are appending the contents of the uibinder-widget to the contents of the RootPanel widget.

    Instead, do this (it is shorter/simpler, and won't have this bug):

    RootPanel.get().add(new TheHandlerTest("My Button"));