@FXML annotation and FXMLLoader class not resolved to a type in Java 11 and JavaFX 11

Satish Pahuja picture Satish Pahuja · Oct 2, 2018 · Viewed 8.3k times · Source

Earlier my project used to be on Java 8 but now I am using Java 11 along with JavaFX 11 and now JavaFX has been decoupled from Java since Java 11. I haven't download the JavaFX SDK but added below dependency in pom.xml for getting required modules and jar files which were used to be part of Java itself in earlier versions.

       <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>11</version>
        </dependency>

But I am getting compilation error can not be resolved to a type on @FXML annotation and FXMLLoader class.

Do I need to add some other/extra dependencies to resolve this issue?

Answer

Jos&#233; Pereda picture José Pereda · Oct 2, 2018

FXML (@FXMLannotation, FXMLLoader...) were moved to the module javafx.fxml with the Java 9 release.

Before JavaFX 11, whenever you were calling something JavaFX related, you had all the javafx modules available within the SDK.

But now you have to include the ones you need:

<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-controls</artifactId>
    <version>11</version>
</dependency>
<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-fxml</artifactId>
    <version>11</version>
</dependency>

Note that javafx.controls depends on javafx.graphics and java.base, so you don't need to include those. And javafx.fxml directly depends on javafx.base.