样本:
private Widget getSelectBox() {
HorizontalPanel hPanel = new HorizontalPanel();
hPanel.setSpacing(20);
final ListBox dropBox = new ListBox(false);
String[] listTypes = {"1", "2", "3"};
for (int i = 0; i < listTypes.length; i++) {
dropBox.addItem(listTypes[i]);
}
dropBox.ensureDebugId("cwListBox-dropBox");
dropBox.getElement().setId("cms-dropBox");
VerticalPanel dropBoxPanel = new VerticalPanel();
dropBoxPanel.setSpacing(4);
dropBoxPanel.add(dropBox);
hPanel.add(dropBoxPanel);
dropBox.addChangeHandler(new ChangeHandler() {
public void onChange(ChangeEvent event) {
Window.alert("change fired");
dropBox.addItem("else");
consoleLog("selected: "+ dropBox.getSelectedIndex());
}
});
return hPanel;
}
ChangeHandler 不起作用。尽管在官方示例中它工作正常(http://samples.gwtproject.org/samples/Showcase/Showcase.html#!CwListBox )。但它在这段代码中不起作用。没有错误。
DOM.sinkEvents((com.google.gwt.user.client.Element) dropBox.getElement(), Event.ONCHANGE );
DOM.setEventListener((com.google.gwt.user.client.Element) dropBox.getElement(), new EventListener() {
@Override
public void onBrowserEvent(Event event) {
if (DOM.eventGetType(event) == Event.ONCHANGE) {
Window.alert("change fired");
dropBox.addItem("who");
consoleLog("selected: "+ dropBox.getSelectedIndex());
}
}
});
这种方式也行不通。
upd:在 MainEntryPoint 类中所有工作,但需要 panel.add(new SomeClass().getWidget()),这不起作用