How to tell IntelliJ get FXML controller from FXMLLoader for syntax hilighting?

1.8k Views Asked by At

My JavaFX project follows these steps: Compute optimal values in Model class initialize Controller create a new instance of FXMLLoader set Controller and view.fxml render View

My fxml file doesn't have a line like fx:controller="com.example.Controller". IntelliJ fails to resolve methods like onMouseClicked="#processMyButtonClick" because it doesn't know what controller to use. No Controller specified for top level element. Is there a way to tell IntelliJ. If controller is specified in FXML Java throws Controller value already specified exception.

As a result source control logs are polluted with false positives "100 errors were found" and a link between #methodName in fxml and public void methodName(){/*..*/} stops working.

Is there a way to tell IntelliJ to derive fxml controller from POJO class?


Based on "No Controller specified for top level element" when programatically setting a Controller one can specify controller in fxml file, but this is undesirable because I would like to initialize Controller before fxml is loaded.

1

There are 1 best solutions below

1
On BEST ANSWER

I also hated this for a long time, but I have found a work around. You specify the Controller in the FXML file and then load it like this:

public static Node loadScene(Controller c) {
    URL resource = Controller.class.getResource("/scene.fxml");
    FXMLLoader loader = new FXMLLoader(resource);
    loader.setControllerFactory(param -> c);
    loader.load();
    return loader.getRoot();
}