0

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 .

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) {

UPDATED

                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;
    }

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());
}
4

2 回答 2

2

您可以使所有复合组件实现具有单个getValue()方法的接口:

public interface HasValue {
    public String getValue();
}

然后,您可以轻松提取实现该接口的所有小部件的值:

    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一个例子(你也可以使用那个接口而不是创建你自己的接口)。

于 2013-01-12T17:49:11.313 回答
0

您只能在 GWT FormPanel 小部件中添加一个小部件,然后您可以使用getWidget()以下方法获取它:

FormPanel panel = createFormWithComponents();

RootPanel.get().add(panel);

VerticalPanel vp = (VerticalPanel) panel.getWidget();
int widgetCount = vp.getWidgetCount();
Window.alert(widgetCount+" ...");
于 2013-01-12T09:58:04.603 回答