Tomasz Mularczyk
Tomasz Mularczyk

Reputation: 36219

Javafx, SceneSwaping

I have a application managing 4 FXML files that work like this: you press next button on layout, and another scene with another FXML file loads. You press another button and so on. Thats a Scene swaping training application. Everything works well until I try to load last(forth) FXML file that has diffrent top Layout(StackPane) than others(AnchorPane). I get exception: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException

This is the code that works for swaping FXML files with AnchorPane top Layout, but throws and exception when I try to load FXML file with StackPane top layout.

private void buttGoToWindow3Action(ActionEvent event) throws IOException{
    Parent window3;
    window3 = (StackPane)FXMLLoader.load(getClass().getResource("/ScenePackage/FXMLWindow3.fxml"));

    Stage mainWindow;
    mainWindow = (Stage)  ((Node)event.getSource()).getScene().getWindow();

    mainWindow.getScene().setRoot(newScene);
}

It seems like mainWindow dont want to accept diffrent Layout. How can I fix it?

Upvotes: 0

Views: 1798

Answers (1)

Tomasz Mularczyk
Tomasz Mularczyk

Reputation: 36219

In a long long exception list Ive found this line: Caused by: javafx.fxml.LoadException: No controller specified.

So I went to the SceneBuilder and set proper controller class for the FXML file.

enter image description here

And it worked, so it was not a casting problem.

Im not sure why its required at this stage of program. Probably because there are methods set inside SceneBuilder(so also inside FXML file)to events and no controller class with methods to execute.

Upvotes: 4

Related Questions