Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get widget by id in gwt

Tags:

dom

gwt

I have a bunch of TextBox-es generated dynamically. At the step of creation I'm assigning the ID property for them. e.g.

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 IDs, but I cannot do it. I tried to use the DOM object in order to get a widget, but it produces an exception:

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

I've also tried to use el.getInnerText, el.getNodeValue - no luck. I have see in the chrome debugger - the textboxes don't have the 'value' property.

like image 437
Andreas Avatar asked Dec 06 '22 18:12

Andreas


2 Answers

you can get the widget associated to an 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;
}
like image 198
Javier Arnáiz Avatar answered Jan 25 '23 02:01

Javier Arnáiz


Since you are constructing your textboxes in gwt java code, why not put them into a map and access them later?

like image 42
Daniel Kurka Avatar answered Jan 25 '23 01:01

Daniel Kurka