Force invalidation of JavaFX properties

2019-07-29 00:31发布

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.)

2条回答
迷人小祖宗
2楼-- · 2019-07-29 01:03

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();
查看更多
Lonely孤独者°
3楼-- · 2019-07-29 01:07

The short answer is yes. However without you being more specific on your use case, there is just some general advice:

  • You can add an InvalidationListener` to your property, which is called whenever the bound property changes it's value
  • You might consider to bind the property bidirectional, so that changes from either direction are reflected at the other side.
  • However bidirectional binding might not be preferable and in that case you can register a ChangeListener to the Property.
查看更多
登录 后发表回答