如果这是一个错误,或者我在JavaFX MapProperty绑定中遗漏了什么,有人可以解释一下吗?
方案: 两个MapProperty实例 - 主和子。
代码:
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)
答案 0 :(得分:1)
MapProperty的值是ObservableMap,而不是ObservableMap的内容。
执行此代码
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>()));
ObservableMap<String, Object> childMap = child.get();
ObservableMap<String, Object> masterMap = master.get();
System.out.println("before binding: " + ((childMap == masterMap) ? "childMap == masterMap" : "childMap != masterMap"));
child.bind(master);
childMap = child.get();
masterMap = master.get();
System.out.println("after binding: " + ((childMap == masterMap) ? "childMap == masterMap" : "childMap != masterMap"));
child.unbind();
System.out.println("after unbinding: " + ((childMap == masterMap) ? "childMap == masterMap" : "childMap != masterMap"));
显示绑定后,child
和main
中的ObservableMap是同一个对象,因为该属性包装了地图而不包含其内容:
before binding: childMap != masterMap
after binding: childMap == masterMap
after unbinding: childMap == masterMap
要绑定地图的内容,请改用bindContent
。执行
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.bindContent(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);
child.unbindContent(master);
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);
给出以下结果:
Java version: 1.8.0_45
OS version : Windows 7 - amd64
------------
master: MapProperty [value: {k1=v1}]
child : MapProperty [value: {k1=v1}]
------------
master: MapProperty [value: {k1=v1}]
child : MapProperty [value: {}]
------------
master: MapProperty [value: {k1=v1}]
child : MapProperty [value: {k2=v2}]