Why does the JavaBean adapter not update the JavaFX property?

admin

Administrator
Staff member
I'm new to JavaFX and need a bidirectional binding between a GUI (using JavaFX properties) and my old Java code (<strong>not</strong> using JavaFX properties). I tried using a JavaBean adapter as the following simple example shows:

Code:
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.adapter.JavaBeanDoubleProperty;
import javafx.beans.property.adapter.JavaBeanDoublePropertyBuilder;

public class App
{
    public static void main(String[] args) throws NoSuchMethodException
    {
        Pojo pojo = new Pojo();
        FXModel model = new FXModel();

        JavaBeanDoubleProperty adapter = JavaBeanDoublePropertyBuilder
                .create().bean(pojo).name("value").build();

        model.valueProperty().bindBidirectional(adapter);

        System.out.println("Adapter before: " + adapter.get());
        System.out.println("Model before: " + model.getValue());
        System.out.println("Bean before: " + pojo.getValue());

        pojo.setValue(123d);

        System.out.println();
        System.out.println("Adapter after: " + adapter.get());
        System.out.println("Model after: " + model.getValue());
        System.out.println("Bean after: " + pojo.getValue());
    }

    public static class Pojo
    {
        private double value;

        public double getValue()
        {
            return value;
        }

        public void setValue(double value)
        {
            this.value = value;
        }
    }

    public static class FXModel
    {
        private final DoubleProperty value = new SimpleDoubleProperty();

        public double getValue()
        {
            return value.get();
        }

        public void setValue(double value)
        {
            this.value.set(value);
        }

        public DoubleProperty valueProperty()
        {
            return value;
        }
    }
}

The generated output is

<blockquote>
Adapter before: 0.0 Model before: 0.0 Bean before: 0.0

Adapter after: 123.0 Model after: 0.0 Bean after: 123.0
</blockquote>

So, by setting the "normal" Java object to a new value the JavaBeanDoubleProperty is informed about the change, but the JavaFX property is not, although it is bound to the adapter. Why?

Even by adding a PropertyChangeSupport to Pojo as described <a href="https://stackoverflow.com/questions/13217407/how-does-javabeans-property-adapter-work">here</a>,

Code:
public static class Pojo
    {
        private double value;
        private PropertyChangeSupport pcs;

        public Pojo()
        {
            pcs = new PropertyChangeSupport(this);
        }

        public double getValue()
        {
            return value;
        }

        public void setValue(double value)
        {
            final double oldValue = this.value;
            this.value = value;
            pcs.firePropertyChange("name", oldValue, value);
        }

        public void addPropertyChangeListener(
                final PropertyChangeListener listener)
        {
            pcs.addPropertyChangeListener(listener);
        }
    },

it does not work. (Unfortunately, a second trial using <a href="http://ugate.wordpress.com/2012/07/30/javafx-programmatic-pojo-expression-bindings-part-iii/" rel="nofollow noreferrer">BeanPathAdapter</a> also did not work.)