本文介绍了通过id在gwt中获取小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一堆TextBox-es动态生成。在创建的步骤中,我为它们分配了ID属性。 例如 id = ... 按钮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;
我需要通过他们的ID访问它们,但我不能这样做我试图使用DOM对象来获取一个小部件,但它会产生一个异常:
String id =some id; 元素el = DOM.getElementById(id); String value = el.getAttribute(值); - 这行生成一个异常我还试图使用el.getInnerText ,el.getNodeValue - 没有运气我在chrome调试器中看到 - 文本框没有'value'属性。
解决方案您可以通过以下方式获取与元素关联的小部件:
public static IsWidget getWidget(com.google.gwt。 user.client.Element element){ EventListener listener = DOM .getEventListener((com.google.gwt.user.client.Element))元素); //没有监听器附加到元素,所以没有这个的元素//元素 if(listener == null){ return null; } if(listener instanceof Widget){ // GWT将该widget用作事件监听器 return(Widget)listener; } 返回null; }
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.
解决方案you can get the widget associated to an element this way:
public static IsWidget getWidget(com.google.gwt.user.client.Element element) { EventListener listener = DOM .getEventListener((com.google.gwt.user.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; }
更多推荐
通过id在gwt中获取小部件
发布评论