1

I'm creating an application in JavaFx in which I'm creating two GridPanes dynimically with same number of rows, both GridPanes have textfields and below there is a Button like this:

enter image description here

User can write any number in GridPane A's dynamically created TextFields like this:

enter image description here

What I want is when user will press Submit button program should calculate sum of values present in each row and then divide each row calculated sum with whole GridPane's TextFields sum and display the result in second GridPane's TextFields according GridPane A row position e.g GridPane A row 0 calculation result should be displayed in GridPane B row 0 like this:

enter image description here

I'm creating GridPane like this GridPane A:

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

Method to return component from table at specific row and column:

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

Button Click implementation:

    @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");

    }
Junaid
  • 664
  • 5
  • 18
  • 35
  • 1
    We are clear what you need but I don't think you have told us what the problem is... – Jai Jun 16 '17 at 00:50
  • I am pretty sure this was answered a couple of days ago. – SedJ601 Jun 16 '17 at 03:27
  • @SedrickJefferson if it's already answered can you please tag the already answered question here ? – Junaid Jun 16 '17 at 07:28
  • Not the same but very similar: https://stackoverflow.com/questions/44413649/javafx-how-to-update-text-of-dynimically-created-textfields-inside-gridpane – SedJ601 Jun 19 '17 at 20:46

1 Answers1

1

I think your approach is on the right way to achieve that, you may try this, when the button is clicked (i.e wrap it with its action listener) (NB. Untested):

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

After OP provided more explanation in the below comments, a few adjustments the above code needs:

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

    }
}
Yahya
  • 13,349
  • 6
  • 30
  • 42
  • Thanks @Yahya for your answer, but I think I didn't explain my question clearly. I'll try to explain again, 1st: I want is calculate sum row by row if your see screenshot 2, in row 1 there are three values: 1,0,0 so the sum of row 1 will be 1+0+0=1, row 2 will be 1+4+0=5, row 3 will be 2+0+1=3, row 4 will be 1+1+2=4. 2nd: I want is to calculate total sum values in GridPane TextFields like according to screenshot the total sum will be 1+0+0+1+4+0+2+0+1+1+1+2=13. Continue in next comment: – Junaid Jun 16 '17 at 20:53
  • 3rd: I want to calculate the average of each row sum with total sum like row 1 average will be row1 avg: row 1 sum/Total sum i.e row1 avg: = 1/13, row 2 avg: 5/13, row 3 avg: 3/13, row 4 avg 4/13. 4th: As you can notice in screenshot 3 I want to display each avg in GridPane B textFields like GridPane A row1 avg result display in GridPane B row1, GridPane A row2 avg result display in GridPane B row2, GridPane A row3 avg result display in GridPane B row3, GridPane A row4 avg result display in GridPane B row4. Hope this time my explanation is clear. – Junaid Jun 16 '17 at 20:53
  • @Junaid After reading the comments, I think very few adjustments the above code needs. I updated my answer. Check it now. – Yahya Jun 16 '17 at 21:50
  • @Junaid I'm glad I could help :) – Yahya Jun 19 '17 at 13:41
  • hi @Yahya , I need your help please regarding [this](https://stackoverflow.com/questions/45816026/javafx-how-to-save-application-data-in-a-xml-file-and-restore-back) – Junaid Aug 22 '17 at 11:33