Java – loads the fxml file located in the “SRC” Maven folder

I am developing a JavaFX 8 (Maven) project I want to store the fxml file in the source (not in the resource) folder

new FXMLLoader(getClass().getResource("/views/b/MyFxml.fxml"));

Is there any way from / SRC / main / Java / package / name / roleview Load my fxml file from the fxml location?

Solution

Java does not distinguish between resources and sources As long as your fxml file is visible in the classpath (properly packaged in *. Jar), you can access it in the same way Since you use maven, this is the configuration you need:

<project>
  ...
  <build>
    ...
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.fxml</include>
        </includes>
      </resource>
      ...
    </resources>
    ...
  </build>
  ...
</project>

This line should then be able to load your file:

new FXMLLoader(getClass().getResource("/package/name/RoleView.fxml"));
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>