【发布时间】:2016-01-14 15:56:19
【问题描述】:
考虑以下程序:
import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
/**
*
* @author kachna
*/
public class ListPropertyTest {
public static void main(String[] args) {
StringProperty p = new SimpleStringProperty();
p.addListener((obs, old, nw) -> {
System.out.println("String Property; oldString: " + old + ", newString: " + nw);
});
p.set("1");
ListProperty<String> listProperty = new SimpleListProperty<>(FXCollections.observableArrayList());
listProperty.addListener((obs, old, nw) -> {
System.out.println("ListProperty; oldList: " + old + ", newList: " + nw);
});
listProperty.addAll("1", "2", "3");
}
}
运行程序给出以下输出:
String Property; oldString: null, newString: 1
ListProperty; oldList: [1, 2, 3], newList: [1, 2, 3]
如您所见,旧值为:
-
null当T是String - 当
T是一个等于新值的值ObservableList<String>
【问题讨论】:
-
对我来说似乎是可变数据的一般问题。当你改变它的内容时,列表实例是相同的,所以旧值和新值是对同一个列表的引用,但是由于通知是在修改之后发生的,所以列表显示新的内容,不管当然,您使用哪个参考来访问它。
-
我调试了它,看起来这来自
com.sun.javafx.binding.ListExpressionHelper.fireValueChangedEvent(change)。此方法赋予旧值和新值相同的值。