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?
You need to add a JavaFX specific accessor variableNameProperty to the controller:
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
Further at line 279 FXMLLoader determines
if (isBindingExpression(value))
then to create binding, instantiatesBeanAdapter
and gets propertyModel:If we look into BeanAdapter
#getPropertyModel()
,it delegates to
BeanAdapter#get()
after appendingString 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.