GWT:如何从 RootPanel 获取对按钮的引用?
我正在使用 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;
}
谢谢,- Dave
I'm using GWT 2.4. In my onModuleLoad method, given a string id, how do I get a reference to an existing button on the page from the RootPanel object? I'm trying this
public void onModuleLoad() {
...
final Button submitButton = (Button) RootPanel.get("submit");
but getting the compile error, "Cannot cast from RootPanel to Button".
Edit:
I thought using an iterator would heal the pain, but no dice. Here is the default HTML loaded (notice the button with 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>
but this code in onModuleLoad causes a NullPointerException (because the submitButton id can't be found) ...
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;
}
Thanks, - Dave
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
Document.get().getElementById("submit").cast()
获取输入元素,但无法获取Button< /code> 从中取出小部件。
如果您将代码更改为读取
You can get your input element using
Document.get().getElementById("submit").<InputElement>cast()
, but you won't be able to get aButton
widget out of it.If you change your code to read
<button type="button" id="submit" name="submit" value="Submit">
instead of<input>
(the type=button part is technically not needed, but some browsers will treat it like a type=submit if you don't), then you can useButton.wrap()
:一些 GWT 小部件具有静态方法wrapp(),它允许将 DOM 元素转换为小部件实例。
按钮提交 = Button.wrap(DOM.getElementById("提交"));
Some of GWT widgets have static method wrap() which allows to convert DOM elements to widget instances.
Button submit = Button.wrap(DOM.getElementById("submit"));
get() 方法返回与浏览器元素关联的 RootPanel,而不是具有该名称的小部件。 RootPanel 是 ComplexPanel 的子类,因此我认为最好的选择是使用 ComplexPanel 中的方法来迭代小部件,从而找到您想要的那个。
The get() method returns the RootPanel associated with the browser element, not the widget with that name. A RootPanel is a subclass of ComplexPanel, so I think your best bet is to use the methods from ComplexPanel to iterate through the widgets and so find the one that you want that way.
Wrap 创建一个图层,并将删除之前在该按钮上编写的所有功能。
我使用下面的代码来解决同样的问题。
Wrap creates a layer and will remove all the functionalities written previously on that button.
I used the below code to resolve the same.