2012-11-16 59 views
1

我使用uibinder創建了自定義SuggestBox,並在使用uibinder的視圖中使用它。如何將onSelection事件從定製小部件發送到uibinder

我的問題是想不出火onSelection事件的看法,我現在用的是自定義suggestbox

下面是我的代碼。

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

您的MySuggestBox不會將SelectionHandler添加到實際的SuggestBox,而是延伸到Composite。當SuggestBox觸發時,它不會在您的建議框中找到添加的處理程序。您可以通過將MySuggestBox中的處理程序添加到實際的建議框字段來修復該問題:

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

非常感謝您......它的工作原理......--) – Sree

相關問題