本文介绍了即使newValue == oldValue也会触发更改事件的JavaFX ObjectProperty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

ObjectPropertyBasenewValue == 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()valueprivate,因此我无法正确覆盖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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 19:01