GWT: How do I get a button link from a RootPanel?

I am using GWT 2.4. In my onModuleLoad method given by the row id, how do I get a link to an existing button on a page from a RootPanel object? I'm trying this

public void onModuleLoad() { ... final Button submitButton = (Button) RootPanel.get("submit"); 

but getting a compilation error, "Cannot be dropped from RootPanel to Button."

Edit:

I thought using an iterator would heal the pain, but not the cubes. The HTML loaded by default is loaded here (note 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 raises a NullPointerException (since submitButton id is not 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

+4
source share
3 answers

You can get your input element using Document.get().getElementById("submit").<InputElement>cast() , but you cannot get the Button widget.

If you change your code to read <button type="button" id="submit" name="submit" value="Submit"> instead of <input> (the type = part is not technically necessary, but some browsers will consider it as a type = submit, if you don't), then you can use Button.wrap() :

 Button button = Button.wrap(Document.get().getElementById("submit")); 
+13
source

Some GWT widgets have static wrap (), which allows you to convert DOM elements to widget instances.

Button submit = Button.wrap (DOM.getElementById ("send"));

+2
source

The get () method returns the RootPanel associated with the browser item, not a widget with that name. RootPanel is a subclass of ComplexPanel, so I believe that it is best to use ComplexPanel methods to iterate through widgets and therefore find the one you want in this way.

+1
source

All Articles