【发布时间】:2011-10-26 16:50:33
【问题描述】:
我正在使用 GWT 2.4。在我的 onModuleLoad 方法中,给定一个字符串 id,我如何从 RootPanel 对象获取对页面上现有按钮的引用?我正在尝试这个
public void onModuleLoad() {
...
final Button submitButton = (Button) RootPanel.get("submit");
但出现编译错误“无法从 RootPanel 转换为 Button”。
编辑:
我认为使用迭代器可以治愈痛苦,但没有骰子。这是加载的默认 HTML(注意带有 id="submit" 的按钮)...
<div>
<form name="f">
File name: <input type="text" size="25" id="filename" name="filename"
value="" /> <input type="button" id="submit" name="submit"
value="Submit" /> <input type="hidden" name="curId" id="curId"
value="" />
</form>
</div>
<div id="content"></div>
但是 onModuleLoad 中的这段代码会导致 NullPointerException(因为找不到 submitButton id)...
public void onModuleLoad() {
final Button submitButton = (Button) getWidgetById("submit");
submitButton.addStyleName("submitButton");
...
private Widget getWidgetById(final String id) {
Widget eltToFind = null;
final Iterator<Widget> iter = RootPanel.get().iterator();
while (iter.hasNext()) {
final Widget widget = iter.next();
final Element elt = widget.getElement();
if (elt.getId() != null && elt.getId().equals(id)) {
eltToFind = widget;
break;
} // if
} // while
return eltToFind;
}
谢谢,- 戴夫
【问题讨论】:
标签: gwt