Je crée une application dans JavaFX où je crée deux GridPanes dynimically avec même nombre de lignes, les deux GridPanes ont textfields et au-dessous il y a un bouton comme ceci:JavaFX: Comment calculer la moyenne à partir des valeurs de TextField créées dynamiquement dans GridPane?
utilisateur peut écrire un nombre dans GridPane a LIKE de TextFields créées dynamiquement ceci:
ce que je veux est lorsque l'utilisateur pressera programme bouton Soumettre doit calculer la somme des valeurs présentes dans chaque ligne, puis diviser chaque ro w somme calculée avec la somme de l'ensemble GridPane TextFields et afficher le résultat dans les TextFields de deuxième GridPane selon GridPane une position de ligne par exemple GridPane Une rangée 0 résultat du calcul doit être affichée dans GridPane B ligne 0 comme suit:
I » m créer GridPane comme ceci 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;
}
Méthode t o retour composante de la table à la ligne spécifique et de la colonne:
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;
}
Bouton mise en œuvre Click:
@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");
}
Nous sommes clairement ce que vous avez besoin, mais Je ne pense pas que vous nous avez dit quel est le problème ... – Jai
Je suis assez sûr que cela a été répondu il y a quelques jours. – Sedrick
@SedrickJefferson Si vous avez déjà répondu, pouvez-vous marquer ici la question déjà posée? – Junaid