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?
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?
The above answer from @invariant is totally correct but for some people that do it that way, there may be performances issues, especially with GridPanes that contain many elements. And also when working with loops (iterating over all the elements of a GridPane).
What I suggest you is to initialize a static array of all your element/nodes contained within the grid pane. Then use this array to get the nodes you need.
i.e.
1. have a double dimensional array :
private Node[][] gridPaneArray = null;
2. Call a method like so during the initialization of your view :
private void initializeGridPaneArray()
{
this.gridPaneArray = new Node[/*nbLines*/][/*nbColumns*/];
for(Node node : this.mainPane.getChildren())
{
this.gridPaneArray[GridPane.getRowIndex(node)][GridPane.getColumnIndex(node)] = node;
}
}
3. Get your node
Node n = this.gridPaneArray[x][y]; // and cast it to any type you want/need