Search code examples
javafxrowgridpane

How to Get ImageView (not Node) from GridPane


I have to get an ImageView from a GridPane.

I use this code, but it is for Node, while I'm trying to change an Image in an ImageView (inside GridPane).

private Node getNodeFromGridPane(GridPane gridPane, int col, int row) {
      for (Node node : gridPane.getChildren()) {
         if (GridPane.getColumnIndex(node) == col && GridPane.getRowIndex(node) == row) {
            System.out.println(node);
            return node;
         }
      }
      return null;
   }

Is there a solution?

I already searched to lots of posts, but there is not this problem solved. Thank you


Solution

  • On caller side you may do the next:

    final Node foundNode = getNodeFromGridPane(gridPane, col, row);
    if (foundNode instanceof ImageView) {
        //do something
    }
    

    instanceof will care for both ImageView and null.