admin管理员组文章数量:1192969
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.
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.
Share Improve this question edited Aug 2, 2011 at 6:17 Rob Fox asked Aug 1, 2011 at 19:49 Rob FoxRob Fox 5,5817 gold badges40 silver badges64 bronze badges4 Answers
Reset to default 15If 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.
Actually, instead of creating a new EventBus or HandlerManager, as your widget will be a subclass of Wiget, it the standard way would be to use the Widget.addHandler(handler, eventType) method. Here's a minimal example:
public class MyWidget<T> extends Composite implements HasValueChangeHandlers<T>, HasValue<T> {
private T value;
public MyWidget() {
// Initialize stuff
}
@Override
public HandlerRegistration addValueChangeHandler(final ValueChangeHandler<T> handler) {
return addHandler(handler, ValueChangeEvent.getType());
}
@Override
public T getValue() {
return value;
}
@Override
public void setValue(T value) {
setValue(value, false);
}
@Override
public void setValue(T value, boolean fireEvents) {
this.value = value;
if (fireEvents) {
ValueChangeEvent.fire(this, getValue());
}
}
}
Really late to answer, but I've just solved this problem like this:
ValueChangeEvent.fire(hasValueChangeHandlerInstance, getValue());
The ValueChangeEvent
is generated for you by GWT. You can add one (or more) ValueChangeHandler
to any class that implements the interface HasValueChangeHandlers
. One of these classes is TextArea
, so let's look at a little example:
TextArea textArea = new TextArea();
textArea.addValueChangeHandler(new ValueChangeHandler<String>() {
@Override
public void onValueChange(ValueChangeEvent<String> event) {
// do something
}
});
When the value of the text area changes, GWT will automatically generate a ValueChangeEvent, which you can use in the part I marked with "do something".
本文标签: javaCustom ValueChangeHandler GWTStack Overflow
版权声明:本文标题:java - Custom ValueChangeHandler GWT - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738426787a2086170.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论