2017-06-15 47 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