I'm trying to add localisation functionality to a JavaFX program using FXML. From what I understand, when you create a FXMLLoader object, you can pass a ResourceBundle into the FXMLLoader's constructor like so:
FXMLLoader loader = new FXMLLoader(Application.class.getResource("main-menu.fxml"),
ResourceBundle.getBundle("com.example.myApp.MainMenu");
However, I want to be able to access the ResourceBundle inside one of my controllers, so that I can do something like this:
class MainMenuController {
// ...
@FXML
protected void onButtonClick() {
welcomeText.setText(localisationBundle.getString("greetMessage");
}
// ...
}
I have put the following initialize method inside my main menu controller...
@FXML
public void initialize(URL location, ResourceBundle bundle) {
this.localisationBundle = bundle;
}
...but it does not get invoked when the program is run, and thus localisationBundle is null when my button is pressed. The initialize method only gets invoked when I change its signature to have no parameters.
I have seen a suggestion on StackOverflow that my controller should implement Initializable, however the documentation says that this has been superceded.
My question is: Is there a way to get access to the ResourceBundle that is passed to the FXMLLoader's constructor? Is Initializable the only way I can do this or is there a better way?
Thanks in advance :)