JavaFX: load resource from other package - NetBeans

偶尔善良 提交于 2019-12-11 09:33:36

问题


I know there have already been some questions about relative paths, but I Keep failing to get JavaFX FXML loader to load a resource from a package other than itself.

The loading class is located in the package gui.controllers and the fxml file BarSheet.fxml is located in the package gui.resources. What should i now put in :

FXMLLoader myLoader = new FXMLLoader(getClass().getResource("src/gui/resources/BarSheet.fxml")); 

Thanks in advance


回答1:


SOLVED: The fault was in the fact that my fxml file contained an error so i didnt know when i entered the right path because it would still not work... The path should start with / to indicate the path starting from the root followed by packages/filename. So in my case

FXMLLoader myLoader = new FXMLLoader(getClass().getResource("/gui/resources/BarSheet.fxml"));



回答2:


As a complement when you have a package like com.company.view, and inside you have the .fxml file in order to make this work you have to put the line like this:

FXMLLoader myLoader = new FXMLLoader(getClass().getResource("/com/company/view/file.fxml"));


来源:https://stackoverflow.com/questions/26452254/javafx-load-resource-from-other-package-netbeans

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