Tomasz Mularczyk
Tomasz Mularczyk

Reputation: 36179

Unable to load FXML when placed in a package

I got this type of file hierarchy in JavaFXApplication5 project.enter image description here

Ive made another package for all the fxml files, so when project gets big it will be easier to find certain files.

Now in JavaFXApplication5 main class I have a line which Im sure causes an exception(java.lang.reflect.InvocationTargetException) when application is trying to run.

AnchorPane root = (AnchorPane) FXMLLoader.load(getClass().getResource("FXMLNew.fxml"));

Im sure its because the "FXMLNew.fxml" root is wrong. But I dont know how to set it when is in another package...?

Or maybe these type of files should be put in normal folder?

Upvotes: 1

Views: 1481

Answers (1)

ItachiUchiha
ItachiUchiha

Reputation: 36722

To load a fxml which is inside a package, use /package-name/fxml-file-name.fxml

For your case:

AnchorPane root = (AnchorPane)FXMLLoader.load(getClass().getResource("/windows/FXMLNew.fxml"));

Upvotes: 1

Related Questions