Search code examples
javajavafxnodesgridpane

.getChildren().add() requires a node and cant add my object


I made a separate class to setup a grid within JavaFX. The class is as follows:

public class Grid { 
    GridPane gp = new GridPane(); //sets grid (10x10)
    gp.setHgap(10);
    gp.setVgap(10);
    gp.setBorder(null);
    gp.setPadding(new Insets(15,15,15,15));

    int[][] shots = new int[10][10];

    for(int i = 0; i<10; i++) {
        for (int j = 0; j < 10; j++) {
            Rectangle r = new Rectangle(40 , 40);
            gp.add(r, j, i);
        }
    }
}

I then have

Group root = new Group();
Grid g = new Grid();
root.getChildren().add(g);

but it throws the following...

The method add(Node) in the type List is not applicable for the arguments (Grid)

I understand that Grid is not a Node type so it can't be added, but I'm really stuck on what I should change to add it. I've tried

public class Grid extends GridPane{}

Which allows the object to be added in the Main, however, it doesn't take the variables added to the gp GridPane in the other class.

Any suggestions?


Solution

  • You were trying to add an object of a class, which have a node as its data member, into the Group. Instead, you have to add the node itself as a child of the Group. Try this way :

    Group root = new Group();
    Grid g = new Grid();
    root.getChildren().add(g.gp);