Custom ValueChangeHandler GWT

I need to write a custom ValueChangeHandler and call onValueChange(ValueChangeEvent) . However, I do not understand how to write a ValueChangeEvent .

Perhaps I misunderstand the whole GWT event system. Can anyone help?

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

The ValueChangeEvent constructor is not displayed, and I cannot create it.

+6
java javascript gwt
Aug 01 2018-11-11T00:
source share
4 answers

If you want to run ValueChangeEvent , you must implement the HasValueChangeHandlers interface or your class or somewhere in the class.

A simple implementation would be to use 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 that you need to replace T type you want to run.

Since you cannot create a ValueChangeEvent, direct event dispatch is performed using the fire method:

 ValueChangeEvent.fire(this, value); 

Where this refers to a class / field that implements HasValueChangeHandlers and value , refers to the value that was changed, and you want to send the event.

+15
Aug 2 '11 at 9:30 a.m.
source share

Actually, instead of creating a new EventBus or HandlerManager, since your widget will be a subclass of Wiget, the standard way would be to use the Widget.addHandler method (handler, eventType). Here is 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()); } } } 
+11
Mar 18 '13 at 12:38
source share

It’s actually too late to answer, but I just solved this problem as follows:

 ValueChangeEvent.fire(hasValueChangeHandlerInstance, getValue()); 
+1
Apr 04 2018-12-12T00:
source share

ValueChangeEvent generated for you by GWT. You can add one (or more) ValueChangeHandler to any class that implements the HasValueChangeHandlers interface. One of these classes is TextArea , so consider a small example:

  TextArea textArea = new TextArea(); textArea.addValueChangeHandler(new ValueChangeHandler<String>() { @Override public void onValueChange(ValueChangeEvent<String> event) { // do something } }); 

When you change the value of the text area, GWT will automatically generate a ValueChangeEvent, which you can use in the part marked "do something."

0
Aug 01 2018-11-21T00:
source share



All Articles