GWT Multiple Events Handling
I have 3 widgets on my UI side (1 ListBox, 2 TextBoxes). I would like to create an Handler which could handle value change event if any of the three widgets value changes and also if on Blur for ListBox.The skeleton of code would be something like this
registerHandler(new multiWidgetHandler());
private class multiWidgetHandler{
//code for handling onValueChange for 3 widgets and also onBlur for listBox
}
开发者_运维问答I am not sure how to implement this cleanly. Need help. Some example code would be appreciated.
You can implement multiple Handler interfaces in the same handler, and then add that handler multiple times.
private class MultiWidgetHandler implements ValueChangeHandler<String>, BlurHandler, ChangeHandler
{
protected void handleIt() { Window.alert("These events are so handled right now!"); }
public void onBlur(BlurEvent e) { handleIt(); }
public void onValueChange(ValueChangeEvent<String> e) { handleIt(); }
public void onChange(ChangeEvent e) { handleIt(); }
}
...
MultiWidgetHandler handler = new MultiWidgetHandler();
listBox.addChangeHandler(handler);
listBox.addBlurHandler(handler);
textArea1.addValueChangeHandler(handler);
textArea2.addValueChangeHandler(handler);
Why are you planning such a behaviour? Usually on handler for one event. If you want to channel all 3 events to a single handler you could make your 3 different handlers call a "global" method : HandleAllEvents(). If all events are of the same type you can register the same handler 3 times.
精彩评论