如何从自定义窗口小部件向uibinder触发onSelection事件

时间:2012-11-16 23:59:49

标签: gwt widget custom-component uibinder suggestbox

我使用uibinder创建了一个自定义SuggestBox,我在使用uibinder的视图中使用它。

我的问题是无法将onSelection事件激发到我使用自定义建议框

的视图

以下是我的代码。

MySuggestBox.ui.xml

<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
xmlns:g="urn:import:com.google.gwt.user.client.ui">
<ui:style>

</ui:style>
<g:SuggestBox ui:field="suggestionBox"></g:SuggestBox>
</ui:UiBinder> 

MySuggestBox.java

public class MySuggestBox extends Composite implements IsWidget, HasSelectionHandlers<Suggestion>{

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

    interface MySuggestBoxUiBinder extends
        UiBinder<Widget, MySuggestBox> {
    }

   @UiField SuggestBox suggestionBox;
   public mySuggestBox(){
       initWidget(uiBinder.createAndBindUi(this));
   }

   @Override
   public HandlerRegistration addSelectionHandler(SelectionHandler<Suggestion> handler)  {
      return addHandler(handler, SelectionEvent.getType());
   }
    ...
    ...
    ...

}

我在这里使用上面的小部件

MySuggestBoxExample.ui.xml

<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
xmlns:g="urn:import:com.google.gwt.user.client.ui
    xmlns:my="urn:import:com.myproject.mywidget">
<ui:style>

</ui:style>
<my:MySuggestBox ui:field="suggestBox" />
</ui:UiBinder> 

MySuggestBoxExample.java

public class MySuggestBoxExample extends Composite implements IsWidget{

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

    interface MySuggestBoxExampleUiBinder extends
        UiBinder<Widget, MySuggestBoxExample> {
    }

   @UiField MySuggestBox suggestBox;
   public MySuggestBoxExample (){
       initWidget(uiBinder.createAndBindUi(this));
   }

   @UiHandler("suggestBox")
   public void onSelection(SelectionEvent<Suggestion> event){
      //I DONT GET THIS EXECUTED
   }
    ...
    ...
    ...

}

我没有触发onSelecion方法。

请帮忙。

1 个答案:

答案 0 :(得分:1)

您的MySuggestBox未将SelectionHandler添加到实际的SuggestBox,而是将其扩展到Composite。当SuggestBox触发时,它不会在您的建议框中找到添加的处理程序。您可以通过将MySuggestBox中的处理程序添加到实际的suggestionbox字段来修复它:

@Override
public HandlerRegistration addSelectionHandler(SelectionHandler<Suggestion> handler)  {
  return suggestionBox.addSelectionHandler(handler);
}