JavaFX:按行和列获取节点
如果我知道它的位置(行和列)或从gridPane获取节点的任何其他方式,有没有办法从gridPane获取特定节点?
我没有看到任何直接的API来获取逐行列索引,但您可以使用来自Pane
getChildren
API,以及来自getRowIndex(Node child)
和getColumnIndex(Node child)
//Gets the list of children of this Parent. public ObservableList 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)
下面是使用GridPane
行和列索引获取Node
的示例代码
public Node getNodeByRowColumnIndex (final int row, final int column, GridPane gridPane) { Node result = null; ObservableList childrens = gridPane.getChildren(); for (Node node : childrens) { if(gridPane.getRowIndex(node) == row && gridPane.getColumnIndex(node) == column) { result = node; break; } } return result; }
重要更新: getRowIndex()
和getColumnIndex()
现在是静态方法,应更改为GridPane.getRowIndex(node)
和GridPane.getColumnIndex(node)
。