how to locate fxml from another package?
Asked Answered
W

3

11

I have created a simple JavaFX application.

enter image description here

It has two packages the main class is JFXTest2.java is in good package and the fxml and it's controller are in JFXTest2 package.

now the problem is that i can not load the fxml in the main class. I tried loading the fxml like this:

Parent root = FXMLLoader.load(getClass().getResource("jfxtest2.Screen.fxml"));

and

Parent root = FXMLLoader.load(getClass().getResource("jfxtest2/Screen.fxml"));

and also

Parent root = FXMLLoader.load(new URL("/jfxtest2/Screen.fxml"));

but none of them worked.So how should i load the fxml from JFXTest2 package in the the JFXTest2 class which is the main class or application class.

Ward answered 7/4, 2014 at 16:49 Comment(0)
M
20

Try

Parent root = FXMLLoader.load(getClass().getResource("/jfxtest2/Screen.fxml"));
Mascagni answered 7/4, 2014 at 17:4 Comment(0)
B
3

You need to use

getClass().getClassLoader().getResource("/jfxtest2/Screen.fxml")
Brawley answered 7/4, 2014 at 17:17 Comment(0)
D
-2

For me nothing worked besides this:

Parent root = FXMLLoader.load(getClass().getResource("..//jfxtest2//Screen.fxml"));
Deepfreeze answered 18/3, 2019 at 21:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.