JavaFX:按行和列获取节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20825935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:37:52  来源:igfitidea点击:

JavaFX: Get Node by row and column

javajavafx

提问by Georgi Georgiev

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?

如果我知道它的位置(行和列)或任何其他方式从 gridPane 获取节点,有没有办法从 gridPane 获取特定节点?

采纳答案by invariant

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

我看不出有任何直接的API来获取由行列索引节点,但可以使用getChildrenAPI从Pane,并getRowIndex(Node child)getColumnIndex(Node child)来自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 Nodeusing row and column indices from the GridPane

这是NodeGridPane

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

重要更新:getRowIndex()andgetColumnIndex()现在是静态方法,应该更改为GridPane.getRowIndex(node)and GridPane.getColumnIndex(node)