I have created a simple JavaFX
application.
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
.
java is in good package and the fxml and it's controller are in JFXTest2 package. Parent root = FXMLLoader. load(new URL("/jfxtest2/Screen. fxml"));
The <fx:include> tag can be used to include one fxml file into another. The controller of the included fxml can be injected into the controller of the including file just as any other object created by the FXMLLoader . This is done by adding the fx:id attribute to the <fx:include> element.
It's possible to use the same controller class for multiple FXML files, but your code will be really hard to follow, and it's a very bad idea. (Also note that using the fx:controller attribute, you will have a different controller instance each time you call FXMLLoader.
Try
Parent root = FXMLLoader.load(getClass().getResource("/jfxtest2/Screen.fxml"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With