0

I have a BorderPane and its center pane is GridPane. The GridPane has 20x20 rectangle objects

 for (int rows = 0; rows < GRID_SIZE; ++rows) {
     for (int cols = 0; cols < GRID_SIZE; ++cols) {
            Rectangle r = new Rectangle(RECTANGLE_SIZE, RECTANGLE_SIZE);
            grid.add(r, rows, cols);

The grid.add method accepts: Node child - r, column and row index.

How could I access the grid using this index

my BorderPane is static for the class

  private static BorderPane bp = new BorderPane();

So when I type bp.getCenter (the grid) I can't find any appropriate method for inserting column and row index, which would return my Rectangle object ?

Edit: solution JavaFX: Get Node by row and column

Community
  • 1
  • 1
Jason Per
  • 139
  • 2
  • 12

1 Answers1

0

You need to use static methods GridPane.getColumnIndex(col) and GridPane.getRowIndex(n). Try this code:

public Optional<Rectangle> findByIndex(GridPane gridPane, int row, int col) {
    final Optional<Rectangle> rectangle = gridPane.getChildren().stream().map(n -> (Rectangle) n).filter(n -> GridPane.getColumnIndex(n) == col && GridPane.getRowIndex(n) == row).findFirst();
    return rectangle;
}
MBec
  • 2,172
  • 1
  • 12
  • 15