Binding a Label's text property (in an FXML file) to an IntegerProperty (in a controller)

devuxer picture devuxer · Nov 6, 2013 · Viewed 24.4k times · Source

I've set up a data binding between a Label in an FXML file and an IntegerProperty in the associated controller. The problem is that, while the label gets set to the correct value upon initialization, it is not updating when the property's value changes.

FXML file

<?xml version="1.0" encoding="UTF-8"?>

<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<GridPane xmlns:fx="http://javafx.com/fxml"
   fx:controller="application.PaneController" minWidth="200">
   <Label id="counterLabel" text="${controller.counter}" />
   <Button translateX="50" text="Subtract 1"
      onAction="#handleStartButtonAction" />
</GridPane>

Controller

package application;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.beans.binding.Bindings;
import javafx.beans.property.*;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;

public class PaneController implements Initializable
{
    private IntegerProperty counter;

    public int getCounter()
    {
        return counter.get();
    }

    public void setCounter(int value)
    {
        counter.set(value);
    }

    public PaneController()
    {
        counter = new SimpleIntegerProperty(15);
    }

    @Override
    public void initialize(URL url, ResourceBundle resources)
    {
    }

    @FXML
    private void handleStartButtonAction(ActionEvent event)
    {
        setCounter(getCounter() - 1);
        System.out.println(getCounter());
    }
}

Expectation

Each time I push the "Subtract 1" button, the counter will decrement by 1, and the counterLabel will update automatically.

Reality

The counter does decrement by 1, but the counterLabel remains stuck at 15 (the initial value).

Question

I was under the impression (e.g., from this forum post) that what I've done should work. What am I missing?

Answer

Uluk Biy picture Uluk Biy · Nov 7, 2013

You need to add a JavaFX specific accessor variableNameProperty to the controller:

public IntegerProperty counterProperty() {
    return counter;
}

EDIT: More details.
The API documentation mentions not so much about this JavaFX's JavaBeans architecture. Just an introduction about it here (Using JavaFX Properties and Binding) but again nothing about its necessity.

So lets dig some source code! :)
Straightforwardly, we start to look into FXMLLoader code first. We notice prefix for binding expression as

public static final String BINDING_EXPRESSION_PREFIX = "${";

Further at line 279 FXMLLoader determines if (isBindingExpression(value)) then to create binding, instantiates BeanAdapter and gets propertyModel:

BeanAdapter targetAdapter = new BeanAdapter(this.value);
ObservableValue<Object> propertyModel = targetAdapter.getPropertyModel(attribute.name);

If we look into BeanAdapter#getPropertyModel(),

public <T> ObservableValue<T> getPropertyModel(String key) {
    if (key == null) {
        throw new NullPointerException();
    }
    return (ObservableValue<T>)get(key + BeanAdapter.PROPERTY_SUFFIX);
}

it delegates to BeanAdapter#get() after appending String PROPERTY_SUFFIX = "Property";
In the get() method, simply the getter (either counterProperty or getCounter or isCounter) invoked by reflection, returning the result back. If the getter does not exist null returned back. In other words, if the "counterProperty()" does not exist in JavaBean, null returned back in our case. In this case the binding is not performed due to the statement if (propertyModel instanceof Property<?>) in FXMLLoader. As a result, no "counterProperty()" method no bindings.

What happens if the getter is not defined in the bean? Again from the BeanAdapter#get() code we can say that if the "getCounter()" cannot be found the null returned back and the caller just ignores it as no-op imo.