0

I'm making the game Minesweeper for a project in class, and I've settled upon making a GridPane, and then adding a Matrix of ImageView objects, i.e.

   GridPane grid = new GridPane();

    ImageView[][] gridButtons = new ImageView[16][16];

    for(int i = 0;i<16;i++)
    {
        for(int k = 0;k<16;k++)
        {
            gridButtons[i][k] = coveredView;
            grid.getChildren().add(gridButtons[i][k]);
        }
    }

However when I try to run it, I get this:

Children: duplicate children added: parent = Grid hgap=0.0, vgap=0.0, alignment=TOP_LEFT

Any help is appreciated, thanks in advance!

Dylan
  • 13,645
  • 3
  • 40
  • 67
C.S.
  • 11
  • 1
  • 3
    You need to create a new instance of `ImageView` in the inner loop instead of using the same `ImageView` over and over again. `Node`s can only be added to a single place in a single scene. You may want to use the `GridPane.add` method btw to specify the column/row indices instead of simply adding all the `ImageView` to the first row/column. – fabian Oct 18 '18 at 15:14
  • You can find another example of the Minesweeper layout in my [answer here](https://stackoverflow.com/questions/52229984/javafx-imageview-actions/52250584#52250584). – Zephyr Oct 20 '18 at 14:14
  • Thank you guys, I think I'll be able to finish this program today, which is a major weight lifted of my shoulders. – C.S. Oct 21 '18 at 20:13

0 Answers0