通过id在gwt中获取小部件

编程入门 行业动态 更新时间:2024-10-20 05:42:04
本文介绍了通过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中获取小部件

本文发布于:2023-11-27 03:46:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636446.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:部件   id   gwt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!