问题描述
我正在使用GWT 2.4。在我的onModuleLoad方法中,如果给定一个字符串ID,我如何从RootPanel对象获取页面上现有按钮的引用?我想这个
public void onModuleLoad(){
...
final Button submitButton = (Button)RootPanel.get(submit);
但是出现编译错误,无法从RootPanel转换到按钮。
编辑:
我认为使用迭代器会治愈痛苦,但不会骰子。这里是默认的HTML加载(注意id =submit的按钮)...
$ p $ < div>
< form name =f>
文件名:< input type =textsize =25id =filenamename =filename
value =/> < input type =buttonid =submitname =submit
value =Submit/> < input type =hiddenname =curIdid =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;
休息;
} // if
} // while
return eltToFind;
谢谢, - Dave
Button
小部件。 如果你更改您的代码以读取< / code>而不是
< code>< button type =buttonid =submitname =submitvalue =Submit >< input>
(类型=按钮部分在技术上是不需要的,但是如果你不这样做,某些浏览器会像type = submit一样),那么你可以使用 Button.wrap()
:
Button button = Button。包裹(Document.get()的getElementById( 提交));
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
You can get your input element using Document.get().getElementById("submit").<InputElement>cast()
, but you won't be able to get a Button
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 use Button.wrap()
:
Button button = Button.wrap(Document.get().getElementById("submit"));
这篇关于GWT:我如何从RootPanel获取对按钮的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!