Working with resources

Let's look again at loading FXML:

FXMLLoader.load(getClass().getResource("FirstDocument.fxml")); // URL

The load() method's parameter here is a URL to the FXML file during runtime. It usually looks like the following:

jar:file:/path/to/jar/file/FxmlDemo.jar!/demo/FirstDocument.fxml

So, you will almost never set it directly as a String but through the getResource() method. The getResource parameter can be relative to the current class, as in the preceding examples, or absolute to your JAR directory structure, for example:

FXMLLoader.load(getClass().getResource("/demo/FirstDocument.fxml"));

Using absolute paths allows you to store FXML files in a separate folder rather than among the Java code, which is more convenient for complex projects.

Often, the project structure will look like the following:

./src/demo/FirstController.java
./src/demo/FxmlDemo.java
./resources/fxmls/FirstController.fxml

Here, the JAR routine will combine both built classes and FXML files into one folder, so the JAR content will look as follows:

FxmlDemo.jar!/demo/FirstController.java
FxmlDemo.jar!/demo/FxmlDemo.java
FxmlDemo.jar!/fxmls/FirstController.fxml

In this case, your FXML loader will have to address the FXML file by an absolute address:

FXMLLoader.load(getClass().getResource("/fxmls/FirstDocument.fxml"));
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.174.253