Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the number of rows in a JavaFX GridPane?

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?

like image 880
j will Avatar asked Dec 24 '13 20:12

j will


2 Answers

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

like image 122
Patrick Avatar answered Oct 01 '22 09:10

Patrick


With java 9, you can do this:

myGridPane.getRowCount();
like image 33
Muzib Avatar answered Oct 01 '22 10:10

Muzib