Reputation: 1431
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.
Upvotes: 2
Views: 1805
Reputation: 2644
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();
}
Upvotes: 6