问题描述
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)
.
Problem: markInvalid()
and value
are private
, therefore I cannot override set(newValue)
properly.
问题::如何获取不进行(value != newValue)
检查的类型?
Question: How can I obtain a type, that does not do the (value != newValue)
check?
此问题与此问题有关
推荐答案
扩展SimpleObjectProperty
(或ObjectPropertyBase
)并覆盖其set
方法并跳过检查.虽然您无法自己调用markInvalid
,但是该方法不能做很多您不能做的事情:
Extend SimpleObjectProperty
(or ObjectPropertyBase
) and override its set
method and skip the check. While you can't call markInvalid
yourself, the method doesn't do much you can't do:
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
方法,您也可以覆盖它.
What we're missing is setting valid
to false
. However, the only place where that matters is in its toString
method, which you can override as well.
这篇关于即使newValue == oldValue也会触发更改事件的JavaFX ObjectProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!