Skip to content

Bind StringProperty to Label from a Singleton

I have a singleton called MenuText. It is responsible for displaying the correct text in the menu. It gets updated dynamically.

public class MenuText implements LanguageObserver {

    private MenuText() { //I want this to be private, only one instance should exist

    private static class MenuTextHolder {
        private static final MenuText INSTANCE = new MenuText();

    public static MenuText getInstance() {
        return MenuTextHolder.INSTANCE;

    public void update(Language language) {
        switch (language) {
            case ENGLISH -> {
            case GERMAN -> {

    private final StringProperty text = new SimpleStringProperty("Play");

    public StringProperty textProperty() {
        return text;

    public String getText() {
        return text.get();

    private void setText(String text) {

I have a fxml file, but the MenuText can’t have a reference to it. (This would contradict the MVVM architectural style)

<?import tiles.text.MenuText?>
<VBox alignment="TOP_CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
      prefHeight="1080.0" prefWidth="1920.0" xmlns="" xmlns:fx=""

<!--here I want to bind to the text property-->
<Button text="???"/>


Initially I used <fx:define> to setup a reference to the MenuText from the fxml file, but this doesn’t allow private constructors. It shouldn’t be that difficult, because MenuText is static, but I’m unable to make a static reference to it’s singleton.

I tried <Button text="${MenuText.getInstance().text}">


As mentioned in this answer, I shouldn’t use the Singleton Pattern. Based on this I added an ApplicationFactory:

//Creation of items with application lifetime
public class ApplicationFactory {

    private Config config;

    public void build() {
        config = new Config();

    public Config getConfig() {
        return config;

Is this the correct approach? I now have a MenuFactory, which gets also created in the JavaFX start() method. It sets the parent of the scene.

public class MenuFactory {
    public Parent getMenu(Config config, String fxmlLocation) {
        MenuText menuText = new MenuText(config);
        MenuViewModel menuViewModel = new MenuViewModel(config);

        try {
            FXMLLoader loader = new FXMLLoader(Objects.requireNonNull(getClass().getResource(fxmlLocation)));
            loader.getNamespace().put("menuText", menuText);
            return loader.load();
        } catch (IOException e) {

The start() mehtod looks like this:

    public void start(Stage primaryStage) {
        ApplicationFactory applicationFactory = new ApplicationFactory();;

        MenuFactory menuFactory = new MenuFactory();

        Parent root = menuFactory.getMenu(applicationFactory.getConfig(), MENU);
        Scene scene = new Scene(root);


This makes it way more complicated and I’m not sure if this is correct. Furthermore, I still don’t know how I set the MenuText in the fxml file. I tried, but I think this isn’t the correct way to set a namespace in fxml.

        <MenuText fx:id="menuText"/>

I read these documentations but don’t understand how I can set this custom namespace.



Be aware that the singleton pattern is widely considered to be an anti-pattern. However, if you really want to do this:

Initially I used <fx:define> to setup a reference to the MenuText from the fxml file

This is the correct approach. You can combine it with fx:factory to get a reference to an instance of a class that does not have a (public) default constructor:

    <MenuText fx:factory="getInstance" fx:id="menuText" />

And then do

<Button text="${menuText.text}" />

Another solution is to “manually” insert the MenuText instance into the FXML namespace:

MenuText menuText = ... ;
FXMLLoader loader = new FXMLLoader(getClass().getResource(...));
loader.getNamespace().put("menuText", menuText);
Parent root = loader.load();

And then

<Button text="${menuText.text}" />

should work with no additional FXML code. This approach allows you to avoid using the singleton pattern (you’re basically injecting the dependency into the FXML, so you could combine it easily with a DI framework).