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?
Get the number of rows in a JavaFX GridPane?
Hej j will, try this method:
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
Isn't rowIndex and rowEnd same numbers? Both are obtained with GridPane.getRowIndex(child). –
Summons
Yeah this doesn't work when I prorammatically add rows to a gridpane it still shows the initial row count, so it has issues –
Delrio
I could have a valid GridPane with a non-empty set of rows, then call getRowConstraints() which is a list, call clear() to clear this list to remove all constraints and then when I call your getRowCount() get a return value of 0 even though the grid still has the same number of rows. –
Divertissement
In my case I used Java Reflections ( GridPane.java has private method getNumberOfRows() ):
Method method = gridPane.getClass().getDeclaredMethod("getNumberOfRows");
method.setAccessible(true);
Integer rows = (Integer) method.invoke(gridPane);
this works after I have added rows in code otherwise all I get is the initial allocation, which is dumb, poor form from the designers of the GridPane class –
Delrio
With java 9, you can do this:
myGridPane.getRowCount();
I believe this returns the row index of the element with the largest row index, so if you have gaps between rows, this doesn't return the number of rows. –
Harquebus
This works for me
GridPane.getRowConstraints().size()
© 2022 - 2024 — McMap. All rights reserved.