Chuck Robertson
Chuck Robertson

Reputation: 17

Can't get sample code to work

I am new to JavaFX coding (in IntelliJ IDEA), and have been reading / searching all over on how to swap scenes in the main controller / container. I found jewelsea's answer in another thread (Loading new fxml in the same scene), but am receiving an error on the following code.

public static void loadVista(String fxml) {
    try {
        mainController.setVista(
                FXMLLoader.load(VistaNavigator.class.getResource(fxml)));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

The error I am receiving is the following:

Error:(56, 27) java: method setVista in class sample.MainController cannot be applied to given types;
  required: javafx.scene.Node
  found: java.lang.Object
  reason: actual argument java.lang.Object cannot be converted to javafx.scene.Node by method invocation conversion

I know other people have gotten this to work, but all I have done is create a new project and copy the code. Can anyone help me?

Upvotes: 2

Views: 59

Answers (1)

James_D
James_D

Reputation: 209553

It looks like you are trying to compile this with JDK 1.7: the code will only work in JDK 1.8 (the difference here being the enhanced type inference for generic methods introduced in JDK 1.8).

You should configure IntelliJ to use JDK 1.8 instead of 1.7.

If you want to try to revert the code to be JDK 1.7 compatible, you can try to replace it with

public static void loadVista(String fxml) {
    try {
        mainController.setVista(
                FXMLLoader.<Node>load(VistaNavigator.class.getResource(fxml)));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

(with the appropriate import javafx.scene.Node ;, if needed). Of course, there may be other incompatibilities since the code you are using is targeted at JDK 1.8.

Upvotes: 1

Related Questions