Java 如何在复合GWT中实现SelectionEvent
我正在GWT中编写一个Java 如何在复合GWT中实现SelectionEvent,java,events,gwt,Java,Events,Gwt,我正在GWT中编写一个Composite小部件,希望它实现HasSelectionHander,并在选择组合元素时触发一个SelectionEvent 到目前为止,我有以下几点: public class SelectionClass extends Composite implements HasSelectionHandlers<Integer>{ private final EventBus eventBus = new SimpleEventBus();
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()));
}
});
// ...
}
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());
}
}
公共类SelectionClass扩展复合实现HASSelectionHandler{
private final EventBus EventBus=新的SimpleEventBus();
//...
发生某些事情(int i){
//如何启动事件?
}
@凌驾
公共句柄注册addSelectionHandler(SelectionHandler){
返回eventBus.addHandler(SelectionEvent.getType(),handler);
}
}
公共另一类{
// ...
SelectionClass.addSelectionHandler(新的SelectionHandler(){
@凌驾
选举时公共无效(SelectionEvent事件){
alert(String.valueOf(event.getSelectedItem());
}
});
// ...
}
我有点困惑于如何准确地实施事件的触发。我是否可以在SelectionClass(如上)中使用
EventBus
。任何帮助都将不胜感激。事件的触发是通过EventBus
API完成的,在GWT 2.4中,您不需要实例化自己的“EventBus”实例,因为您可以将addXXXHandler
方法委托给superComposite
类
应该是这样的:
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()));
}
});
// ...
}
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());
}
}
公共类SelectionClass扩展复合实现HASSelectionHandler{
//...
发生某些事情(int i){
选择Event.fire(此,i);
}
@凌驾
公共句柄注册addSelectionHandler(SelectionHandler){
返回super.addHandler(handler,SelectionEvent.getType());
}
}
SelectionEvent的构造函数是,所以我不知道;I don’我不认为我可以按照你的建议构造这个对象吗?对不起,是用来处理GWT 2.4之前的版本的。更新了我的答案