2017-06-15 39 views
1

我创建在JavaFX中,我创建了两个GridPanes应用dynimically具有相同的行数,都GridPanes具有文本字段和下面有这样一个按钮:JavaFX:如何从GridPane内的动态创建的TextField的值来平均平均值?

enter image description here

用户可以写任何数量的GridPane A的动态创建的TextField这样的:

enter image description here

我要的是,当用户将按下提交按钮的程序应该算目前各行中值的总和,然后将每个RO瓦特与全GridPane的TextField的总和计算出的和,并显示结果在根据GridPane甲行位置例如GridPane甲行0的计算结果第二GridPane的TextField的应显示在GridPane乙行0是这样的:

enter image description here

I”米这样GridPane甲创建GridPane:

public static GridPane tableA(int rows, Button button){ 
    GridPane table = new GridPane(); 

    rowValidationBindings = new BooleanBinding[rows]; 

    for(int i=0; i<rows; i++){ 
     TextField textField1 = new TextField(); 
     textField1.setAlignment(Pos.CENTER); 
     TextField textField2 = new TextField(); 
     textField2.setAlignment(Pos.CENTER); 
     TextField textField3 = new TextField(); 
     textField3.setAlignment(Pos.CENTER); 

     rowValidationBindings[i] = Bindings.createBooleanBinding(
      () -> { 
       if (textField1.getText().matches("\\d+") && 
        textField2.getText().matches("\\d+") && 
        textField1.getText().matches("\\d+") { 
        return true ; 
       } else { 
        return false ; 
       } 
      }, textField1.textProperty(), textField2.textProperty(), textField3.textProperty() 
     ); 

     table.add(textField1, 0, i+1); 
     table.add(textField2, 1, i+1); 
     table.add(textField3, 2, i+1); 
    } 

    button.disableProperty().bind(Bindings.createBooleanBinding(
     () -> ! Stream.of(rowValidationBindings).allMatch(BooleanBinding::get), 
     rowValidationBindings 
    )); 

    return table; 
} 

GridPane乙

public static GridPane tableB(int rows, Button button){ 
    GridPane table = new GridPane(); 

    rowValidationBindings = new BooleanBinding[rows]; 

    for(int i=0; i<rows; i++){ 
     TextField textField1 = new TextField(); 
     textField1.setAlignment(Pos.CENTER); 

     rowValidationBindings[i] = Bindings.createBooleanBinding(
      () -> { 
       if (textField1.getText().matches("\\d+") { 
        return true ; 
       } else { 
        return false ; 
       } 
      }, textField1.textProperty() 
     ); 

     table.add(textField1, 0, i+1); 
    } 

    button.disableProperty().bind(Bindings.createBooleanBinding(
    () -> ! Stream.of(rowValidationBindings).allMatch(BooleanBinding::get), 
    rowValidationBindings 
)); 
    return table; 
} 

方法吨

public static Node getComponent (int row, int column, GridPane table) { 
     for (Node component : table.getChildren()) { // loop through every node in the table 
      if(GridPane.getRowIndex(component) == row && 
          GridPane.getColumnIndex(component) == column) { 
       return component; 
      } 
     } 

     return null; 
    } 

按钮点击执行::○在具体的行和列从表中返回组件

@FXML 
    private void calcul() { 
     GridPane table = (GridPane) anchorPane.getChildren().get(0); 
     for(int i=1 ; i<=comboxBox.getValue(); i++){ 
      String text0 = ((TextField) getComponent (i, 0, table)).getText(); 
      String text1 = ((TextField) getComponent (i, 1, table)).getText(); 
      String text2 = ((TextField) getComponent (i, 2, table)).getText(); 

       System.out.println(text0 + " " + text1 + " " + text2); 
       System.out.println("Next Row"); 

    } 
+1

我们很清楚自己需要什么,但我不认为你已经告诉我们问题是什么... – Jai

+0

我很确定这是几天前回答的。 – Sedrick

+0

@SedrickJefferson如果它已经回答了,请您在此标记已经回答的问题? – Junaid

回答

1

我觉得你的做法是在实现这一正确的方式,你可以试试这个,当按钮被点击(即换行动监听器)(注意。未经测试):

// pass the numberOfRows which is comboxBox.getValue() 
// pass tableA and tableB. 
// fetch the numbers (in a String format) from all TextFields at every row in tableA 
// and caclulate the result after parsing the Strings from each as double values 
// set the result in the corresponding TextField in tableB in every loop 
private void calcul(GridePane tableA, GridPane tableB, int numberOfRows) { 
    double result = 0; 
    for(int i=0 ; i<numberOfRows; i++){ 
     result = (Double.parseDouble(((TextField) getComponent (i, 0, tableA)).getText()) + 
        Double.parseDouble(((TextField) getComponent (i, 1, tableA)).getText()) + 
        Double.parseDouble(((TextField) getComponent (i, 2, tableA)).getText())) 
              /(numberOfRows*3); 

     ((TextField) getComponent (i, 0, tableB)).setText(String.valueOf(result)); 

    } 
} 

UPDATE

后OP在下面的评论提供更多的解释,一些调整上面的代码需要:

// pass the numberOfRows which is comboxBox.getValue() 
// pass tableA and tableB. 
private void calcul(GridePane tableA, GridPane tableB, int numberOfRows) { 
    // first get the total and store it in a variable 
    double total =0; 
    for(Node node : tableA){ 
     if(node instanceof TextField){ 
     total += Double.parseDouble(((TextField)node).getText()); 
     } 
    } 

    // fetch the numbers (in a String format) from all TextFields at every row in tableA 
    // and calculate the average after parsing the Strings from each as double values 
    // set the average in the corresponding TextField in tableB in every loop 
    double average = 0; 
    for(int i=0 ; i<numberOfRows; i++){ 
     average = (Double.parseDouble(((TextField) getComponent (i, 0, tableA)).getText()) + 
        Double.parseDouble(((TextField) getComponent (i, 1, tableA)).getText()) + 
        Double.parseDouble(((TextField) getComponent (i, 2, tableA)).getText())) 
             /(total); 

     ((TextField) getComponent (i, 0, tableB)).setText(String.valueOf(average)); 

    } 
} 
+0

谢谢@Yahya您的答案,但我想我没有清楚地解释我的问题。我会再试一次解释,第一:如果你看截图2,我想要的是逐行计算总和,第一行有三个值:1,0,0,所以第一行的总和将是1 + 0 + 0 = 1,行2将是1 + 4 + 0 = 5,行3将是2 + 0 + 1 = 3,行4将是1 + 1 + 2 = 4。第二:我想要根据截图计算GridPane TextFields中的总和值,总和为1 + 0 + 0 + 1 + 4 + 0 + 2 + 0 + 1 + 1 + 1 + 2 = 13。在下一个评论中继续: – Junaid

+0

第三:我想计算每行总数与总数的平均值,如第1行平均值将是第1行平均值:第1行总和/总和即第1行平均值:= 1/13,第2行平均值:5/13,第3行平均:3/13,第4行平均4/13。4th:正如你可以在截图3中看到的那样,我希望在GridPane B中显示每个平均值textFields,例如GridPane在GridPane B中row1,avg结果显示row1,GridPane GridPane中row2 avg结果显示row2,GridPane GridPane中row3 avg结果显示B row3,GridPane GridPane B row4中的row4 avg结果显示。希望这次我的解释清楚。 – Junaid

+0

@Junaid在阅读完评论后,我认为上面的代码需要很少的调整。我更新了我的答案。现在检查。 – Yahya