如何为GWT列表框显式触发OnChangeHandler?
在我的场景中,当我调用ListBox的SetSelectedIndex(X)方法时应该触发它。
答案 0 :(得分:4)
尝试任何一个:
覆盖setSelectedIndex()
方法并触发ChangeEvent
final ListBox lb = new ListBox() {
@Override
public void setSelectedIndex(int index) {
super.setSelectedIndex(index);
DomEvent.fireNativeEvent(Document.get().createChangeEvent(), this);
}
};
OR
覆盖setSelectedIndex()
方法并调用用于ChangeHandler
同样行为的方法。
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()));
}