GWT事件处理程序未触发

时间:2016-03-31 10:27:28

标签: java gwt event-handling widget

你有什么线索为什么事件处理程序不会触发?
也许我在事件冒泡和gwt上遇到了问题?

public class MyNavBarUI extends Composite
{
      @UiTemplate("MyNavBarUI.ui.xml")
      interface MyUiBinder extends UiBinder<Widget, MyNavBarUI>
      {}

      @UiField
      HTML html;
      @UiField
      HTMLPanel htmlp;

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

      public MyNavBarUI()
      {
        // sets listBox
        initWidget(uiBinder.createAndBindUi(this));

        html.setHTML(
                         "<div id = \"ul_ud1\"> Home </div> "
                       + "<div id = \"ul_ud2\"> News </div>"
                       + "<div id = \"ul_ud3\"> Contact </div>"
                       + "<div id = \"ul_ud4\"> About </div>"
                   );
      }

      @Override
      public void onAttach()
      {
         super.onAttach();

         Event.setEventListener(htmlp.getElementById("ul_ud1"), new EventListener() {

            @Override
            public void onBrowserEvent(Event event) {

                Window.alert(""+event.toString());      
            }
        });

      }
}

1 个答案:

答案 0 :(得分:0)

你只需要下沉事件:

Event.sinkEvents(htmlp.getElementById("ul_ud1"), Event.ONCLICK);

您可以收集多个事件,例如

Event.sinkEvents(htmlp.getElementById("ul_ud1"), Event.ONCLICK | Event.ONMOUSEOVER);

然后,在EventListener中,您可以检查事件的类型:

public void onBrowserEvent(Event event) {
    switch(event.getTypeInt()) {
        case Event.ONCLICK:
            Window.alert("Click");
            break;
        case Event.ONMOUSEOVER:
            Window.alert("Mouse over");
            break;
    }
}