How do I explicitly fire the OnChangeHandler for GWT listbox?
In my scenario it should be fired when I call the SetSelectedIndex(X) method of ListBox.
Try any one:
Override setSelectedIndex()
method and fire the ChangeEvent
final ListBox lb = new ListBox() {
@Override
public void setSelectedIndex(int index) {
super.setSelectedIndex(index);
DomEvent.fireNativeEvent(Document.get().createChangeEvent(), this);
}
};
OR
Override setSelectedIndex()
method and call the same method that is used for ChangeHandler
giving same behavior.
public void onModuleLoad() {
// Make a new list box, adding a few items to it.
final ListBox lb = new ListBox() {
@Override
public void setSelectedIndex(int index) {
super.setSelectedIndex(index);
onChangeBody(this);
}
};
lb.addItem("foo");
lb.addItem("bar");
lb.addItem("baz");
lb.addItem("toto");
lb.addItem("tintin");
lb.addChangeHandler(new ChangeHandler() {
@Override
public void onChange(ChangeEvent event) {
onChangeBody(lb);
}
});
// Make enough room for all five items (setting this value to 1 turns it
// into a drop-down list).
lb.setVisibleItemCount(5);
// Add it to the root panel.
RootPanel.get().add(lb);
}
public void onChangeBody(ListBox lb) {
System.out.println(lb.getValue(lb.getSelectedIndex()));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With