问题
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