【发布时间】:2015-06-05 09:32:58
【问题描述】:
谁能解释一下这是一个错误还是我在 JavaFX MapProperty 绑定中遗漏了什么?
场景: 两个 MapProperty 实例 - 主实例和子实例。
- 首先我们将孩子绑定到主人
- 然后我们在master中存储一些值
- 将子节点与主节点解除绑定
- 清除孩子
- 两个实例都是空的 - 为什么?
- 在 child 中存储一些值
- 两个实例包含相同的值 - 为什么?
代码:
public static void main(String[] args) {
MapProperty<String, Object> master = new SimpleMapProperty<String, Object>(
FXCollections.observableMap(new HashMap<String, Object>()));
MapProperty<String, Object> child = new SimpleMapProperty<String, Object>(
FXCollections.observableMap(new HashMap<String, Object>()));
child.bind(master);
master.put("k1", "v1");
System.out.println("Java version: " + System.getProperty("java.version"));
System.out.println("OS version : " + System.getProperty("os.name") + " - " + System.getProperty("os.arch"));
System.out.println("------------");
System.out.println("master: " + master);
System.out.println("child : " + child);
// Isn't this supposed to stop change listener ?????
child.unbind();
child.clear();
System.out.println("------------");
System.out.println("master: " + master);
System.out.println("child : " + child);
child.put("k2", "v2");
System.out.println("------------");
System.out.println("master: " + master);
System.out.println("child : " + child);
}
输出:
run:
Java version: 1.8.0_45
OS version : Windows 7 - amd64
------------
master: MapProperty [value: {k1=v1}]
child : MapProperty [bound, invalid]
------------
master: MapProperty [value: {}]
child : MapProperty [value: {}]
------------
master: MapProperty [value: {k2=v2}]
child : MapProperty [value: {k2=v2}]
BUILD SUCCESSFUL (total time: 0 seconds)
【问题讨论】:
标签: java javafx-8 property-binding