In my projects I came up with a new naming convention:
If a class "mypackage.<name>" loads a FXML file, then the FXML file should be in the same package and be named "<name>.fxml".
One advantage is, that when following this naming convention it's quite easy to see, which FXML loader/ fx:root-controller and FXML file belong together.
Another advantage is, that when using a utility method, the loading code for eg. a fx:root-based mypackage/MyPane.fxml file (a construct I can highly recommend) such as:
can be simplified to this:
This code will:
- set the ClassLoader
- set the root
- set the controller
- set the ResourceBundle by looking for a mypackage/MyPane.properties file (or a locale specific derivation using the default Locale)
- set the location to mypackage/MyPane.fxml
- load the mypackage/MyPane.fxml file
- reduce code duplication
- reduce typos
The FXMLLoaders utility class provides several other utility methods taking advantage of this naming convention, such as utility methods for loading non-fx:root based FXML files, which then for a mypackage/MyApplication.fxml file such as:
can simplify the loading code to this:
Other variants allow to specify an alternative ResourceBundle or a pre-existing FXMLLoader.
The FXMLLoaders utility class is provided by the Drombler Commons - FX - Core artifact:
Like the other Drombler Commons artifacts, it can be used inside and outside of an OSGi environment.
Hello Mr Brunner,
ReplyDeleteNice blog! I am editor at Java Code Geeks (www.javacodegeeks.com). We have the JCG program (see http://www.javacodegeeks.com/join-us/jcg/), that I think you’d be perfect for.
If you’re interested, send me an email to eleftheria[dot]kiourtzoglou[at]javacodegeeks[dot]com and we can discuss further.
Best regards,
Eleftheria Kiourtzoglou