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?
You can get the row index and column index by utilising the static methods getRowIndex() and getColumnIndex() which are located in the GridPane class. System. out. println("Row: " + GridPane.
To retrieve the coordinates from a node, you just need to invoke the GridPane static methods getColumnIndex() and getRowIndex() .
GridPane lays out its children within a flexible grid of rows and columns. If a border and/or padding is set, then its content will be layed out within those insets. A child may be placed anywhere within the grid and may span multiple rows/columns.
add(button6, 2, 1, 1, 1); The first parameter of the add() method is the component (node) to add to the GridPane . The second and third parameter of the add() method is the column index and row index of the cell in which the component should be displayed. Column and row indexes start from 0.
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)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With