0

I created a GridPane in my JFX Fluidon Designer and I'm trying to reference it in one of my java files.

More specifically, I'd like to programatically put it inside a Vbox with an Hbox I also created in Java. I'll post the code below, it might make more sense.

When I run this code, I get an error that my gridPane variable is null when I use it in the addAll method.

Thanks in advance!

public class MyProgram extends Application {

Label mStatus;
ImageView img_x;
ImageView img_o;

@FXML public GridPane gridPane;

@Override
public void start(Stage primaryStage) {
    BorderPane root = new BorderPane();

    img_x = new ImageView("file:x.png");
    img_o = new ImageView("file:o.png");
    img_x.setUserData("X");
    img_o.setUserData("O");

    HBox hbox = new HBox();
    hbox.getChildren().addAll(img_x, img_o);

    VBox vbox = new VBox();
    vbox.getChildren().addAll(hbox, gridPane);

    root.setCenter(vbox);
Sam
  • 1
  • FXML files should have a controller. You can only call @FXML public GridPane gridPane; in the controller associated with the FXML file. – SedJ601 Mar 09 '17 at 06:27
  • as @SedrickJefferson points out, to inject the gridPane in some class using the FXML tag, you must set that class to be the fxml controller. Then you will be able to reference any fxml node by its fx:id – alvaro Mar 09 '17 at 10:54
  • To clarify the comment from @alvaro: you should have a *separate class* that is specified as the controller class in the FXML file: [do not use the `Application` class as the controller class](http://stackoverflow.com/questions/32081713/javafx-controller-class-not-working). The other thing missing from the question is any code where you actually load the FXML... – James_D Mar 09 '17 at 12:50
  • Thank you guys, I was able to figure it out. I'm new to javaFX and got all tangled up trying to get the variables to the application class where I needed them – Sam Mar 09 '17 at 20:06

0 Answers0