本文介绍了自定义ValueChangeHandler GWT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我需要编写一个自定义 ValueChangeHandler 并调用 onValueChange(ValueChangeEvent)。但我不明白如何编写 ValueChangeEvent

可能我理解整个GWT事件系统是错误的。任何人都可以提供帮助吗?



编辑:我在问如何创建自己的派发ValueChangeEvent的类。我知道如何听它。



ValueChangeEvent的构造函数不可见,我无法创建它。

解决方案

如果您想触发 ValueChangeEvent ,您必须实现接口 HasValueChangeHandlers

一个简单的实现就是使用EventBus:

  EventBus bus = new SimpleEventBus(); 

@Override
public void fireEvent(GwtEvent<?> event){
bus.fireEvent(event);

$ b @Override
public HandlerRegistration addValueChangeHandler(ValueChangeHandler< T>处理程序){
return bus.addHandler(ValueChangeEvent.getType(),handler);

请注意,您需要替换 T

由于您无法直接创建ValueChangeEvent,因此通过fire方法调度事件:

  ValueChangeEvent.fire(this,value); 

其中这个指向类/字段实现 HasValueChangeHandlers 指的是已更改的值,并且您想分派该事件。


I need to write a custom ValueChangeHandler and call out onValueChange(ValueChangeEvent). However I don't understand how to write a ValueChangeEvent.

Maybe I understand the entire GWT event system wrong. Can anyone help?

Edit: I am asking how to create my own class that dispatches the ValueChangeEvent. I understand how to listen to it.

The constructor of ValueChangeEvent is not visible and I can't create it.

解决方案

If you want to fire a ValueChangeEvent you must implement the interface HasValueChangeHandlers by or your class or somewhere in the class.

A simple implementation would be to use the EventBus:

EventBus bus = new SimpleEventBus();

@Override
public void fireEvent(GwtEvent<?> event) {
    bus.fireEvent(event);
}

@Override
public HandlerRegistration addValueChangeHandler(ValueChangeHandler<T> handler) {
    return bus.addHandler(ValueChangeEvent.getType(), handler);
}

Note you need to substitute T with the type you want to fire.

Because you can't create a ValueChangeEvent directly dispatching an event is done via the fire method:

ValueChangeEvent.fire(this, value);

Where this refers to the class/field implementing the HasValueChangeHandlers and value refers to the value that has been changed and you want to dispatch the event.

这篇关于自定义ValueChangeHandler GWT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 06:17