问题
ObjectPropertyBase
skips value invalidation when 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();
}
}
Problem: markInvalid()
and value
are private
, therefore I cannot override set(newValue)
properly.
Question: How can I obtain a type, that does not do the (value != newValue)
check?
This question is related to this question.
回答1:
How can I obtain a type, that does not do the (value != newValue) check?
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();
}
}
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.
来源:https://stackoverflow.com/questions/45116003/javafx-objectproperty-that-fires-change-events-even-if-newvalue-oldvalue