javafx GridPane检索特定的Cell内容

Eli*_*ias 14 javafx cell gridpanel

我想检索Gridpane中一个特定单元格的内容.我把按钮放在了细胞中

setConstraints(btt , 0 ,1 ) 

setConstraints(btt , 0 ,2 )

getChildren().add....
Run Code Online (Sandbox Code Playgroud)

在我的情况下,这GridPane.getChildren.get(10)是不好的.我想直接进入单元格(4,2)并获取其内容.

Shr*_*ave 23

好吧,我想如果没有解决方案从gridpane获取特定节点的是列和行索引,我有一个函数来做到这一点,

private Node getNodeFromGridPane(GridPane gridPane, int col, int row) {
    for (Node node : gridPane.getChildren()) {
        if (GridPane.getColumnIndex(node) == col && GridPane.getRowIndex(node) == row) {
            return node;
        }
    }
    return null;
}
Run Code Online (Sandbox Code Playgroud)

  • 非常奇怪的是,在这些年之后,java不提供基本功能,你必须使用这些无效的解决方法来使基本的东西工作.但我想这是基础的基础数据结构. (4认同)

Ahm*_* Tn 6

假设您有一个8x8 girdPane,其中i是行并且j是列,您可以编写:

myGridPane.getChildren().get(i*8+j)

返回类型是一个对象,所以你必须抛出它,在我的例子中它是:

(StackPane) (myGridPane.getChildren().get(i*8+j))

  • 在某些情况下可能会出现这种情况,但子列表不一定按行/列的顺序维护,稍后可能会使用GridPane.setRowIndex()等进行更改... (2认同)