JavaFX: 行と列でノードを取得する 質問する

JavaFX: 行と列でノードを取得する 質問する

位置 (行と列) がわかっている場合、gridPane から特定のノードを取得する方法はありますか。または、gridPane からノードを取得する他の方法はありますか。

ベストアンサー1

行と列のインデックスでノードを取得するための直接的なAPIは見当たりませんが、、およびのgetChildrenAPIを使用できます。PanegetRowIndex(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)

Node以下は、行と列のインデックスを使用して取得するサンプルコードです。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;
}

重要な更新: getRowIndex()およびは静的メソッドになったので、およびgetColumnIndex()に変更する必要があります。GridPane.getRowIndex(node)GridPane.getColumnIndex(node)

おすすめ記事