2017-06-15 3 views
1

I Iは、行数が同じでdynimically 2 GridPanesを作成していた中でのJavaFXでアプリケーションを作成しています、両方GridPanesは、テキストフィールドを持っており、以下、このようなボタンがあります:JavaFX:ダイナミックに作成されたTextFieldの値から平均を計算する方法は?

enter image description here

ユーザーが書き込むことができますこのようなGridPane Aの動的に作成テキストフィールド内の任意の数:ユーザーがボタンのプログラムが各行に存在する値の合計を計算し、各ROを分割すべき送信]を押しますとき

enter image description here

私が欲しいものです全体GridPaneのテキストフィールドの和で計算和wおよび第二GridPaneのテキストフィールドに結果を表示GridPane行位置に応じて、例えばGridPane行0の計算結果は次のようGridPane B行0に表示されるべきである:

enter image description here

I」はM本GridPane Aよう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 B

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

法T O特定の行と列のテーブルからコンポーネントを返します。

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

、あなたはこれを試すことがあり、ときボタン(、つまりアクションリスナでラップします)(NB。未テスト):OPは以下のコメントでより詳細な説明を提供した後

// 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

は、いくつかの調整上記のコードは必要:

// 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ですが、私は私の質問をはっきりと説明していないと思う。私はもう一度説明しようとします。第1回:私はあなたのスクリーンショット2を参照して、行1で3つの値があります:行1の合計は1 + 0 + 0 = 1、行2は1 + 4 + 0 = 5、行3は2 + 0 + 1 = 3、行4は1 + 1 + 2 = 4となります。第2回:GridPane TextFieldの総計値をスクリーンショットに従うように計算することが、1 + 0 + 0 + 1 + 4 + 0 + 2 + 0 + 1 + 1 + 1 + 2 = 13になります。 – Junaid

+0

第3回:各行の平均を計算したいのですが、1行目の平均値は1行目の平均値です.1行目の合計値/ 1行目の合計値.1行目の平均値:= 1/13、2行目の平均値:5/13、行3平均:3/13、行4平均4/13。第4回:スクリーンショット3で気付くように、私は各平均値をGridPaneに表示したい。B TextFieldsはGridPaneのようなものである.Allは結果表示をGridPaneに、Bはrow1を、GridPaneはRow2を結果の表示をGridPaneに、Bはrow2を、GridPaneはRowPartの結果をGridPane B row3、GridPane GridPane B row4のrow4の平均結果を表示します。今回私の説明は明らかです。 – Junaid

+0

@Junaidコメントを読んだ後、上記のコードに必要な調整はほとんどないと思います。私は自分の答えを更新しました。今すぐチェックしてください。 – Yahya

関連する問題