0

I'm dynamically generating a square GridPane with an equal number of columns and rows. I'm populating each index of the GridPane with a Rectangle object. All I need to do is change the color of one of these Rectangle objects at a specific (column,row) index. I see two ways of solving this problem.

  1. Retrieve the object and directly change it's color witht the setFill() method.

  2. Remove the rectangle at the target index, create a new rectangle, and insert the new one.

If anyone has any ideas on how either of these could be done I would greatly appreciate it.

1

1 Answer 1

0

You could of course store those Rectangles in a Rectangle[][] array with the array indices matching the GridPane indices, which allows you to easily access the element using the given indices.

You could however also identify the child using the layout parameters of GridPane

for (Node child : gridPane.getChildren()) {
    Integer r = GridPane.getRowIndex(child);
    Integer c = GridPane.getColumnIndex(child);
    int row = r == null ? 0 : r;
    int column = c == null ? 0 : c;
    if (row == searchRow && column == searchColumn && (child instanceof Rectangle)) {
        Rectangle rect = (Rectangle) child;

        // do something with rect

        break;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.