Javafx 2 : How do I delete a row or column in Gridpane
Asked Answered
L

4

6

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.

Lustful answered 11/4, 2014 at 2:38 Comment(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.Prole
A
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.

Ammieammine answered 11/4, 2014 at 2:49 Comment(2)
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...Algology
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.Ammieammine
O
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.)

Ortensia answered 7/12, 2017 at 0:32 Comment(1)
this should be an ||, the row index cant be null and 0 at the same timeRevanche
D
2

This works pretty well:

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

while(MainGridPane.getColumnConstraints().size() > 0){
    MainGridPane.getColumnConstraints().remove(0);
}
Dalston answered 7/4, 2017 at 11:29 Comment(0)
C
2

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

Chas answered 2/2, 2022 at 19:20 Comment(1)
targetRowIndexIntegerObject starts from 1, right? Then, the last line should be grid.getRowConstraints().remove(targetRowIndex - 1).Rugging

© 2022 - 2024 — McMap. All rights reserved.