JavaFX: Get Node by row and column

前端 未结 1 634
余生分开走
余生分开走 2020-11-28 12:46

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?

相关标签:
1条回答
  • 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).

    0 讨论(0)
提交回复
热议问题