I’m using what seems to be a typical JavaFX start() method when loading from an FXML file:
public void start(Stage stage) throws Exception {
this.stage = stage;
Scene scene = new Scene(FXMLLoader.<AnchorPane>load(getClass().getResource("layout.fxml")));
stage.setScene(scene);
stage.show();
}
I have some variables that correspond to controls, like:
@FXML private TextField userName;
I would like to initialize userName to a value from the Preferences, as in:
prefs = Preferences.userRoot().node(this.getClass().getName());
userName.setText(prefs.get("userName", userName.getText()));
But when can I call this? If I do it before the stage.show(), userName hasn’t yet been instantiated.
TIA
Make your Controller implement
Initializableand put loading toinitialize():Also note that your can use
ResourceBundleand put your defaults directly tofxmlfile. Just createmy.propertiesfile and use it during loading:then in your fxml you may use properties from
my.properties: