SimpleStringProperty set() vs. setValue()

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 04:46:03

set/setValue and get/getValue methods pairs exist to align Object properties with primitive types properties like BooleanProperty or DoubleProperty:

BooleanProperty:

void set(boolean value)
void setValue(java.lang.Boolean v)

DoubleProperty:

void set(double value)
void setValue(java.lang.Number v)

In these property classes ___Value methods work with corresponding to type objects while direct methods work with primitive types.

Looking in the code you may find a bit of a difference in the logic. For example, DoubleProperty#setValue(null) is equal to DoubleProperty#set(0.0) (which was required by binding). So generally I'd advise to use set/get methods and leave setValue/getValue to binding needs as they may incorporate additional logic.

For Object/String properties there is no difference between set and setValue methods.

StringProperty.java :

@Override
public void setValue(String v) {
    set(v);
}

StringPropertyBase.java:

@Override
public void set(String newValue) {
    if (isBound()) {
        throw new java.lang.RuntimeException("A bound value cannot be set.");
    }
    if ((value == null)? newValue != null : !value.equals(newValue)) {
        value = newValue;
        markInvalid();
    }
}

In common case, you can open sources from open javafx and see that.

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