how to locate fxml from another package?

瘦欲@ 提交于 2019-12-30 04:29:05

问题


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.


回答1:


Try

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



回答2:


You need to use

getClass().getClassLoader().getResource("/jfxtest2/Screen.fxml")



回答3:


For me nothing worked besides this:

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


来源:https://stackoverflow.com/questions/22918418/how-to-locate-fxml-from-another-package

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!