获取 JavaFX GridPane 中的行数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20766363/
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
Get the number of rows in a JavaFX GridPane?
提问by j will
I initialized a GridPane through SceneBuilder and inside the controller I want to conditionally add a row to the GridPane. I do not want to store an int for how many rows I initialized, I want to be able to get the number of rows from the GridPane object. Is that possible?
我通过 SceneBuilder 初始化了一个 GridPane,在控制器内部我想有条件地向 GridPane 添加一行。我不想为我初始化的行数存储一个 int,我希望能够从 GridPane 对象中获取行数。那可能吗?
采纳答案by Patrick
Hej j will, try this method:
hej j会,试试这个方法:
private int getRowCount(GridPane pane) {
int numRows = pane.getRowConstraints().size();
for (int i = 0; i < pane.getChildren().size(); i++) {
Node child = pane.getChildren().get(i);
if (child.isManaged()) {
Integer rowIndex = GridPane.getRowIndex(child);
if(rowIndex != null){
numRows = Math.max(numRows,rowIndex+1);
}
}
}
return numRows;
}
This worked for me.
这对我有用。
Patrick
帕特里克
回答by Vova Perebykivskyi
In my case I used Java Reflections ( GridPane.java has private method getNumberOfRows()):
就我而言,我使用了 Java 反射( GridPane.java 有私有方法getNumberOfRows()):
Method method = gridPane.getClass().getDeclaredMethod("getNumberOfRows");
method.setAccessible(true);
Integer rows = (Integer) method.invoke(gridPane);
回答by Muzib
With java 9, you can do this:
使用 java 9,你可以这样做:
myGridPane.getRowCount();
回答by Tarkhan
This works for me
这对我有用
GridPane.getRowConstraints().size()