JavaFX: Get Node by row and column


Question

Is there any way to get a specific Node from a gridPane if I know its location (row and column) or any other way to get a node from a gridPane?

1
26
12/29/2013 2:06:59 PM

Accepted Answer

I don't see any direct API to get node by row column index, but you can use getChildren API from Pane, and getRowIndex(Node child) and getColumnIndex(Node child) from GridPane

//Gets the list of children of this Parent. 
public ObservableList<Node> getChildren() 
//Returns the child's column index constraint if set
public static java.lang.Integer getColumnIndex(Node child)
//Returns the child's row index constraint if set.
public static java.lang.Integer getRowIndex(Node child)

Here is the sample code to get the Node using row and column indices from the GridPane

public Node getNodeByRowColumnIndex (final int row, final int column, GridPane gridPane) {
    Node result = null;
    ObservableList<Node> childrens = gridPane.getChildren();

    for (Node node : childrens) {
        if(gridPane.getRowIndex(node) == row && gridPane.getColumnIndex(node) == column) {
            result = node;
            break;
        }
    }

    return result;
}

Important Update: getRowIndex() and getColumnIndex() are now static methods and should be changed to GridPane.getRowIndex(node) and GridPane.getColumnIndex(node).

34
12/27/2017 11:28:34 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon