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?