Question

If I want to add a row of text fields programatically in JavaFx, i can simply use the gridpane add method

This adds a set of text fields to row 1.

for (int i = 0; i < Fields.size(); i++) {
   gridpane.add(new TextField(), i, 1);
}

Similarly, How do I delete a row?. I dont find a suitable method to delete a row/column conveeniently in JavaFX.

Was it helpful?

Solution

There's no directly equivalent method. To remove nodes, just use gridpane.getChildren().remove(...); or gridpane.getChildren().removeAll(...); and pass in the nodes you want to remove from the pane.

OTHER TIPS

In Java 8+, you can use removeIf:

gridPane.getChildren().removeIf(node -> GridPane.getRowIndex(node) == rowNumber);

Caveat
If removing items from the 0th row, also check GridPane.getRowIndex(node) == null, i.e.,

node -> GridPane.getRowIndex(node) == null || GridPane.getRowIndex(node) == 0

(I think this is JavaFX leaving the row number as null when no row number is given in the corresponding element in FXML, even though giving no row number in FXML means the element is in the 0th row, since the default row is the 0th row.)

This works pretty well:

while(MainGridPane.getRowConstraints().size() > 0){
    MainGridPane.getRowConstraints().remove(0);
}

while(MainGridPane.getColumnConstraints().size() > 0){
    MainGridPane.getColumnConstraints().remove(0);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top