ObjectPropertyBase
在 newValue == oldValue
时跳过值失效:
/**
* {@inheritDoc}
*/
@Override
public void set(T newValue) {
if (isBound()) {
throw new java.lang.RuntimeException((getBean() != null && getName() != null ?
getBean().getClass().getSimpleName() + "." + getName() + " : ": "") + "A bound value cannot be set.");
}
if (value != newValue) {
value = newValue;
markInvalid();
}
}
问题: markInvalid()
和 value
是 private
,因此我无法覆盖 set( newValue)
正确。
问题:如何获取一个不执行(value != newValue)
检查的类型?
此问题与 this question 相关.
最佳答案
How can I obtain a type, that does not do the (value != newValue) check?
扩展SimpleObjectProperty
(或ObjectPropertyBase
)并覆盖其set
方法并跳过检查。虽然您无法自己调用 markInvalid
,但该方法不会做很多您做不到的事情:
class InvalidatingObjectProperty<T> extends SimpleObjectProperty<T> {
@Override
public void set(T newValue) {
if (isBound()) {
throw new java.lang.RuntimeException(
(getBean() != null && getName() != null ? getBean().getClass().getSimpleName() + "." + getName() + " : " : "")
+ "A bound value cannot be set.");
}
invalidated();
fireValueChangedEvent();
}
}
我们缺少的是将valid
设置为false
。但是,唯一重要的地方是其 toString
方法,您也可以重写该方法。
关于即使 newValue == oldValue 也会触发更改事件的 JavaFX ObjectProperty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45116003/