2013-12-18 33 views
12

我想檢索Gridpane中某個特定單元格的內容。我已經把按鈕放在單元格中,javafx GridPane修復特定單元格內容

setConstraints(btt , 0 ,1) 

setConstraints(btt , 0 ,2) 

getChildren().add.... 

在我的情況下,GridPane.getChildren.get(10)不好。我想直接進入單元格(4,2)並獲取內容。

+0

非常感謝您的回覆悠逸這 –

回答

19

嗯,我想如果沒有解決從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; 
} 
2

假設你有一個8x8 girdPane其中i是行和j是列,你可以寫:

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

返回類型是一個對象,所以你必須要投它,在我的情況是:

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

+1

可能在某些情況下,真實的,但孩子列表不一定保持在行/列的順序,並且可與GridPane.setRowIndex()等後來改變.. – Adam

+0

我從來沒有使用過setRowIndex(),所以我不能說太多。這個解決方案適用於我,我相信在大多數情況下,當你不改變默認的行/列索引時,應該完成工作。 –