JavaFX ObjectProperty that fires change events even if newValue == oldValue

依然范特西╮ 提交于 2019-12-24 18:51:41

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!