Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX: Get Node by row and column

Tags:

java

javafx

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?

like image 950
Georgi Georgiev Avatar asked Dec 29 '13 14:12

Georgi Georgiev


People also ask

How do you get the column and row index of a node in 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.

How do I get node from GridPane?

To retrieve the coordinates from a node, you just need to invoke the GridPane static methods getColumnIndex() and getRowIndex() .

How does GridPane work in JavaFX?

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.

How do I add nodes to GridPane in JavaFX?

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.


1 Answers

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

like image 109
invariant Avatar answered Oct 03 '22 01:10

invariant