Search code examples
javajavafxbindingpropertiesinvalidation

Force invalidation of JavaFX properties


I have a class with the getters/setters for interacting with an underlying device driver. The getter reads data from device and the setter writes data to device. I would add a FX property to this class in order to bind to a GUI controls. I build the property by using JavaBeanPropertyBuilder and it's works. But the property value can be changed not only on GUI side but also on a device side, so I need update the properties by a timer and I want force invalidate the properties in order to update all bindings. Is it possible?

UPD with code example:

class MyDevice {
    public double getSpeed() {
        return (double)driver.getParameter("speed");
    }
    public void setSpeed(double value) {
        driver.setParameter("speed", value);
    }
    private DoubleProperty speed = new JavaBeanDoublePropertyBuilder().bean(this).name("speed").build();
    public DoubleProperty speedProperty() {
        return speed;
    }
}

Now, I bind MyDevice::speedProperty to GUI control and if a value has been changed on the driver side I want something like speedProperty.invalidate() to force handle all registered listeners and update the bindings. (I know about changes on the driver side by a timer's querying of a device status, for example, but not all available parameters.)


Solution

  • The fireValueChangedEvent method can be used to notify the JavaBeanDoubleProperty that the underlying data has been changed.

    private JavaBeanDoubleProperty speed = new JavaBeanDoublePropertyBuilder().bean(this).name("speed").build();
    
    speed.fireValueChangedEvent();