Tyler
Tyler

Reputation: 55

fx:id and initialize() not running in javafx

I am trying to create a user interface, starting with a grid. I built the grid in scenebuilder and I now want to use my controller to add columns and rows. However, my program does not seem to run the initialise() in my controller as the grid does not change size. This is my main class:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;    import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {
    //@Override 
    public void start(Stage primaryStage) {
        try {
            int width = 7;
            int height = 7;         
            final FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("GUI.fxml"));
            loader.setController(new GUIController(width, height));
            final Parent root = 
            FXMLLoader.load(getClass().getResource("GUI.fxml"));    
            final Scene scene = new Scene(root);
            primaryStage.setTitle("GUI");
            primaryStage.setScene(scene);
            primaryStage.show();            
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

This is my controller class:

import javafx.fxml.FXML;
import javafx.scene.layout.GridPane;

public class GUIController {

    private int width;
    private int height;

    public GUIController(int givenWidth, int givenHeight) {//runs this
        width = givenWidth;
        height = givenHeight;
    }


    @FXML 
    public void initialize() { //doesn't run this
        SetGrid.build(gridpane, width, height);
    }   

This is my first time writing in javafx so I may have made simple mistakes, sorry.

Upvotes: 0

Views: 1745

Answers (1)

James_D
James_D

Reputation: 209330

You are calling the static FXMLLoader.load(URL) method. Since it's a static method, it doesn't actually reference the FXMLLoader instance you created, and so it doesn't reference the controller you set.

Call the instance method load() with no parameters instead:

final Parent root = loader.load();

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {
    @Override 
    public void start(Stage primaryStage) {
        try {
            int width = 7;
            int height = 7;         
            final FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("GUI.fxml"));
            loader.setController(new GUIController(width, height));

            final Parent root = loader.load();

            final Scene scene = new Scene(root);
            primaryStage.setTitle("GUI");
            primaryStage.setScene(scene);
            primaryStage.show();            
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Upvotes: 5

Related Questions