强制JavaFX属性失效 [英] Force invalidation of JavaFX properties
问题描述
我有一个带有getters / setter的类,用于与底层设备驱动程序交互。 getter从设备读取数据,setter将数据写入设备。我会为这个类添加一个FX属性,以便绑定到GUI控件。我使用JavaBeanPropertyBuilder构建属性,它的工作原理。但是属性值不仅可以在GUI端更改,还可以在设备端更改,因此我需要通过计时器更新属性,并且我希望强制使属性无效以更新所有绑定。有可能吗?
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 代码示例:
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;
}
}
现在,我绑定 MyDevice :: speedProperty
到GUI控件,如果在驱动程序端更改了值,我想要像 speedProperty.invalidate()
那样强制处理所有注册的侦听器并更新绑定。 (例如,我知道计时器查询设备状态时驾驶员方面的变化,但不是所有可用参数。)
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.)
推荐答案
fireValueChangedEvent
方法可用于通知 JavaBeanDoubleProperty
基础数据已被更改。
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();
这篇关于强制JavaFX属性失效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!