Get widget by id in gwt

I have a bunch of TextBox-es dynamically generated. In the creation step, I assign an ID property to them. eg.

id = ... Button b = new Button(); b.setText("add textbox"); b.addClickHandler(new Clickhandler() { Textbox tb = new TextBox(); tb.getElement().setId(Integer.toString(id)); tb.setText("some text"); } id += 1; 

I need to access them later by their identifiers, but I cannot do this. I tried using the DOM object to get the widget, but it throws an exception:

 String id = "some id"; Element el = DOM.getElementById(id); String value = el.getAttribute("value"); - this line produces an exception. 

I also tried using el.getInnerText, el.getNodeValue - no luck. I see in chrome debugger - text fields do not have value property.

+4
source share
3 answers

you can get the widget associated with the element this way:

 public static IsWidget getWidget(com.google.gwt.dom.client.Element element) { EventListener listener = DOM .getEventListener((com.google.gwt.dom.client.Element) element); // No listener attached to the element, so no widget exist for this // element if (listener == null) { return null; } if (listener instanceof Widget) { // GWT uses the widget as event listener return (Widget) listener; } return null; } 
+10
source

Since you are creating your text fields in gwt java code, why not put them on a map and access them later?

+4
source

Keep in mind the difference between an attribute and a property in HTML / DOM. In your example, “value” is a property. You can try the # getPropertyString element. It used to be that attributes and properties were used interchangeably, but in modern browsers this is no longer the case.

+1
source

All Articles