1

我想将所有网格面板存储到一个数组列表中,以便我可以在必要时遍历它们。然而,得到的结果并不出乎我的意料。使用 Netbeans 调试模式,我意识到无论添加到 arraylist 的网格面板,它的值都与从 jsp 绑定的值不同。

例如,(从调试模式)

Name                    Type                  Value
gridPanel1              HtmlPanelGrid         #334
.
.
gridPanels              ArrayList_            #323
[0]                     HtmlPanelGrid         #524

问题是什么是错的?正确的方法是什么?

JSP 文件

<h:panelGrid binding="#{CreatePage.gridPanel1}" columns="10" id="gridPanel1" style="height: 100%" width="671">
    <webuijsf:label for="column_TF1" id="columnLabel1" text="Column 1: "/>
    <webuijsf:textField binding="#{CreatePage.column_TF1}" id="column_TF1" valueChangeListenerExpression="#{CreatePage.column_TF1_processValueChange}"/>
    <webuijsf:label for="dataDropDown1" id="dataLabel1" text="Data Type: "/>
    <webuijsf:dropDown binding="#{CreatePage.dataDropDown1}" id="dataDropDown1"
                                    items="#{CreatePage.dataDropDown1DefaultOptions.options}"
                                    onChange="webui.suntheme4_2.common.timeoutSubmitForm(this.form, 'mainGridPanel:columnGridPanel:gridPanel1:dataDropDown1');"
                                    selected="#{CreatePage.dataDropDown1DefaultOptions.selectedValue}" valueChangeListenerExpression="#{CreatePage.dataDropDown1_processValueChange}"/>
    <webuijsf:textField binding="#{CreatePage.charValueTF1}" columns="5" id="charValueTF1" rendered="false"/>
    <webuijsf:label for="pkDropDown1" id="pkLabel1" text="Primary Key: "/>
    <webuijsf:dropDown binding="#{CreatePage.pkDropDown1}" id="pkDropDown1"
                                    items="#{CreatePage.pkDropDown1DefaultOptions.options}" onChange="" selected="#{CreatePage.pkDropDown1DefaultOptions.selectedValue}"/>
    <webuijsf:label for="nullableDropDown1" id="nullableLabel1" text="Nullable:"/>
    <webuijsf:dropDown binding="#{CreatePage.nullableDropDown1}" id="nullableDropDown1"
                                    items="#{CreatePage.pkDropDown1DefaultOptions.options}" onChange=""
                                    selected="#{CreatePage.pkDropDown1DefaultOptions.selectedValue}" valueChangeListenerExpression="#{CreatePage.nullableDropDown1_processValueChange}"/>
</h:panelGrid>
<h:panelGrid binding="#{CreatePage.gridPanel2}" columns="10" id="gridPanel2" style="height: 100%" width="671">
    <webuijsf:label for="column_TF2" id="columnLabel2" text="Column 2: "/>
    <webuijsf:textField binding="#{CreatePage.column_TF2}" id="column_TF2" valueChangeListenerExpression="#{CreatePage.column_TF2_processValueChange}"/>
    <webuijsf:label for="dataDropDown2" id="dataLabel2" text="Data Type: "/>
    <webuijsf:dropDown binding="#{CreatePage.dataDropDown2}" id="dataDropDown2"
                                    items="#{CreatePage.dataDropDown1DefaultOptions.options}"
                                    onChange="webui.suntheme4_2.common.timeoutSubmitForm(this.form, 'mainGridPanel:columnGridPanel:gridPanel2:dataDropDown2');" selected="#{CreatePage.dataDropDown1DefaultOptions.selectedValue}"/>
    <webuijsf:textField binding="#{CreatePage.charValueTF2}" columns="5" id="charValueTF2" rendered="false"/>
    <webuijsf:label for="pkDropDown2" id="pkLabel2" text="Primary Key: "/>
    <webuijsf:dropDown binding="#{CreatePage.pkDropDown2}" id="pkDropDown2"
                                    items="#{CreatePage.pkDropDown1DefaultOptions.options}" onChange="" selected="#{CreatePage.pkDropDown1DefaultOptions.selectedValue}"/>
    <webuijsf:label for="nullableDropDown2" id="nullableLabel2" text="Nullable:"/>
    <webuijsf:dropDown binding="#{CreatePage.nullableDropDown2}" id="nullableDropDown2"
                                    items="#{CreatePage.pkDropDown1DefaultOptions.options}" onChange=""
                                    selected="#{CreatePage.pkDropDown1DefaultOptions.selectedValue}" valueChangeListenerExpression="#{CreatePage.nullableDropDown1_processValueChange}"/>
</h:panelGrid>

Java 文件

List<HtmlPanelGrid> gridPanels = new ArrayList<HtmlPanelGrid>();

public HtmlPanelGrid getGridPanel1() {
    return gridPanel1;
}

public void setGridPanel1(HtmlPanelGrid hpg) {
    this.gridPanel1 = hpg;
}
private HtmlPanelGrid gridPanel2 = new HtmlPanelGrid();

public HtmlPanelGrid getGridPanel2() {
    return gridPanel2;
}

public void setGridPanel2(HtmlPanelGrid hpg) {
    this.gridPanel2 = hpg;
}
private HtmlPanelGrid gridPanel3 = new HtmlPanelGrid();

if (gridPanels.isEmpty()) {
        gridPanels.add(gridPanel1);
        gridPanels.add(gridPanel2);
        gridPanels.add(gridPanel3);
        gridPanels.add(gridPanel4);
        gridPanels.add(gridPanel5);
        gridPanels.add(gridPanel6);
}

for (int i = 0; i < gridPanels.size(); i++) {
        TextField tempTF = (TextField) gridPanels.get(i).findComponent(("column_TF" + Integer.toString(i)));

        // I always get null value
        tempStr = (String) tempTF.getValue();
    }
4

1 回答 1

1

The reason why I keep getting null value is because I am not getting the value from the right place.

The correct code should be

for (int i = 0; i < gridPanels.size(); i++) {
    // first get() returns the gridPanel
    // second get() returns the components within the gridPanel. 
    // In this case, 1 is the TextField
    TextField tempTF = (TextField) gridPanels.get(i).getChildren().get(1);

    tempStr = (String) tempTF.getValue();
}
于 2013-06-26T07:31:07.960 回答