5

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.

Natty
  • 185
  • 1
  • 1
  • 9
  • 1
    Have you set some size constraints on the columns or rows? Because this may cause the columns to take space even when they're empty. – SasQ Apr 27 '16 at 10:32

4 Answers4

11

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.

James_D
  • 201,275
  • 16
  • 291
  • 322
  • 3
    This doesn't seem to remove the row or column from the pane, if I try to shrink down the grid by removing the `Nodes` from the final rows and columns, the other rows and columns don't expand to fill the space, the rows and columns are still there... – Troyseph Jun 24 '15 at 07:16
  • 2
    In JavaFX, layout is top-down; i.e. the space taken up by a `GridPane` (and its positioning) is determined by its parent node. So that sounds like a problem with how you have managed layout (but also maybe you have the wrong row and/or column constrains on your `GridPane`). You should probably post a question showing exactly what you are trying to do, with some code. – James_D Jun 24 '15 at 12:19
5

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.)

Ruben9922
  • 766
  • 1
  • 11
  • 16
2

This works pretty well:

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

while(MainGridPane.getColumnConstraints().size() > 0){
    MainGridPane.getColumnConstraints().remove(0);
}
Neuron
  • 5,141
  • 5
  • 38
  • 59
0

JavaFX APIs are pretty lacking (like easily removing rows from GridPane) and unintuitive (like returning null instead 0 for GridPane.getRowIndex). Here is solution I came up with:

Utils:

package io.github.againpsychox.javaspeedrunsapp.utils;

import javafx.scene.Node;
import javafx.scene.layout.GridPane;

public class GridPaneUtils {
    /**
     * Gets row index constrain for given node, forcefully as integer: 0 as null.
     * @param node Node to look up the constraint for.
     * @return The row index as primitive integer.
     */
    public static int getRowIndexAsInteger(Node node) {
        final var a = GridPane.getRowIndex(node);
        if (a == null) {
            return 0;
        }
        return a;
    }

    /**
     * Removes row from grid pane by index.
     * Note: Might not work correctly if row spans are used.
     * @param grid Grid pane to be affected.
     * @param targetRowIndexIntegerObject Target row index to be removed. Integer object type, because for some reason `getRowIndex` returns null for children at 0th row.
     */
    public static void removeRow(GridPane grid, Integer targetRowIndexIntegerObject) {
        final int targetRowIndex = targetRowIndexIntegerObject == null ? 0 : targetRowIndexIntegerObject;

        // Remove children from row
        grid.getChildren().removeIf(node -> getRowIndexAsInteger(node) == targetRowIndex);

        // Update indexes for elements in further rows
        grid.getChildren().forEach(node -> {
            final int rowIndex = getRowIndexAsInteger(node);
            if (targetRowIndex < rowIndex) {
                GridPane.setRowIndex(node, rowIndex - 1);
            }
        });

        // Remove row constraints
        grid.getRowConstraints().remove(targetRowIndex);
    }
}

Example usage:

GridPaneUtils.removeRow(this.grid, GridPane.getRowIndex(this.idTextField));

Posting my solution for further readers...

AgainPsychoX
  • 1,527
  • 1
  • 16
  • 20
  • targetRowIndexIntegerObject starts from 1, right? Then, the last line should be grid.getRowConstraints().remove(targetRowIndex - 1). – user27772 Jan 20 '23 at 13:49