1

我正在 GWT 中编写一个小部件,并希望它在选择复合元素时Composite实现HasSelectionHander并触发SelectionEvent

到目前为止,我有以下内容:

public class SelectionClass extends Composite implements HasSelectionHandlers<Integer>{

    private final EventBus eventBus = new SimpleEventBus();

    //...   

    private void somethingHappens(int i){
            //How do i fire an event?
    }       

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

}

public AnotherClass{

    // ...  

    SelectionClass.addSelectionHandler(new SelectionHandler<Integer>() {

            @Override
            public void onSelection(SelectionEvent<Integer> event) {
                    Window.alert(String.valueOf(event.getSelectedItem()));
            }       
    });     

    // ...  
}

我对如何准确地触发事件感到有些困惑。我EventBus在 SelectionClass 中使用 an 是否正确(上图)。任何帮助,将不胜感激。

4

1 回答 1

2

事件的触发是通过EventBusAPI 完成的,在 GWT 2.4 中,您不需要实例化您自己的“EventBus”实例,因为您可以将您的addXXXHandler方法委托给超Composite类。

它会是这样的:

public class SelectionClass extends Composite implements HasSelectionHandlers<Integer>{

    //...   

    private void somethingHappens(int i){
            SelectionEvent.<Integer>fire(this, i);
    }       

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

}
于 2013-05-28T14:33:54.503 回答