GWT:从FormPanel/VerticalPanel中检索动态添加的组件 [英] GWT: retrieve Components from FormPanel/VerticalPanel which are added dynamically
问题描述
我正在使用GWT 2.5.0创建一个网页,在此我使用了许多复合组件.并且该网页是基于XML开发的.
I am creating a web page using GWT 2.5.0, in this I used lots of composite component. And the web page is developed based on XML .
因此,在这里我们解析XML,并根据每个元素将componnet添加到VerticalPanel,最后将其添加到FormPanel,然后将其返回以添加到RootPanel.在该组件对应于XML的旁边,我添加了一个Button,将其称为提交"按钮,在其click事件上,我想获取该表单中每个组件的值. 为了明确我的想法,下面是我的工作的伪代码:
So here we parse the XML and according to each element add componnet to a VerticalPanel and finally add it to a FormPanel and then return it to add to RootPanel. Beside the Component corresponds to XML I add a Button, call it submit button, on its click event I want to get the values of every component in that form. To clear what my idea is , below is the pseudo code of my work:
public FormPanel createFormWithComponents(){
FormPanel fp = new FormPanel();
vp = new VerticalPanel();
//these are creatred based on the xml
ABC coutn = new ABC (null,"pressure count",true);
PI propotion = new PI(12.5,"HR percentage");
vp.add(coutn);
vp.add(propotion);
//a common button to every form created here
Button submit = new Button("submit");
vp.add(submit);
submit.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
已更新
String values = "";
Iterator<Widget> vPanelWidgets = vp.iterator();
while (vPanelWidgets.hasNext()){
Widget childWidget = vPanelWidgets.next();
if(childWidget instanceof ABC){
ABC xx = (ABC )childWidget;
values = String.valueOf(xx.getMagnitude());
}
}
Window.alert(values);
}
});
fp.add(vp);
return fp;
}
任何见识将不胜感激. 更新: 这里是将每个子窗口小部件组件与我的Composite组件之一进行比较,与所有Composite组件相比,我是否必须这样做?我有什么简单或优化的方法吗? 我想要一个很好的解决方案,因为那里有很多复合组件:
Any insight would be appreciated. UPDATED: Here am comparing each of child widget component with one of my Composite component , do i have to do like this comparing to all Composite component? I there any kind of simple or optimized way to do it? I want good solution for doing this process as there are so many composite component over there:
if(childWidget instanceof ABC ){
ABC xx = (ABC )childWidget;
values = String.valueOf(xx.getMagnitude());
}
推荐答案
您可以使所有Composite组件实现具有单个getValue()
方法的接口:
You could make all you Composite components implement an interface having a single getValue()
method:
public interface HasValue {
public String getValue();
}
然后,您可以轻松提取实现该接口的所有小部件的值:
Then you can easilly extract the value of all widgets implementing that interface:
while (vPanelWidgets.hasNext()){
Widget childWidget = vPanelWidgets.next();
if(childWidget instanceof HasValue){
HasValue xx = (HasValue) childWidget;
values = xx.getValue();
}
}
如果需要返回字符串以外的值,则可以使用泛型.看一下GWT的com.google.gwt.user.client.ui.HasValue
作为示例(您也可以使用该接口而不是创建自己的接口).
If you need to return values other than strings, you could use generics. Take a look at GWT's com.google.gwt.user.client.ui.HasValue
for an example of that (you could also use that interface instead of creating your own).
这篇关于GWT:从FormPanel/VerticalPanel中检索动态添加的组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!