作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试放置一个与我 View 上的监听器链接的 ObservableMap> 。
这是我正在使用的代码:
ObservableMap<Integer, HashMap<String, Integer>> map = FXCollections.observableHashMap();
map.addListener((MapChangeListener.Change<? extends Integer, ? extends HashMap<String, Integer>> change) -> {
System.out.println("Changed key: " + change.getKey());
});
HashMap<String, Integer> store1 = new HashMap<>();
store1.put("apple", 100);
store1.put("strawberry", 123);
store1.put("lemon", 165);
map.put(1, store1);
HashMap<String, Integer> store2 = new HashMap<>();
store2.put("peach", 45);
store2.put("blackberry", 90);
store2.put("melon", 10);
map.put(2, store2);
HashMap<String, Integer> cpStore2 = map.get(2);
cpStore2.put("peach", 40);
map.put(2, cpStore2);
如果我执行它,我会得到这个:
Changed key: 1
Changed key: 2
所以我的问题是,当我在 map 上进行更新时,没有触发任何事件。我确实需要这个。
有人知道我该怎么做吗?
最佳答案
如果值发生变化,该事件就会被触发。但你用相同的键放置相同的 map 。所以很明显没有更改事件。为了确保触发事件,您可以使用现有值创建一个新 map 并放置新值:
HashMap<String, Integer> cpStore2 = new HashMap<String, Integer>(map.get(2));
// cpStore2 is another map than store2 but with the same values
cpStore2.put("peach", 40);
map.put(2, cpStore2);
关于java - HashMap 的 ObservableMap 作为值类型不触发 MapChangeListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44280011/
我正在尝试放置一个与我 View 上的监听器链接的 ObservableMap> 。 这是我正在使用的代码: ObservableMap> map = FXCollections.observable
我尝试在 JavaFX 中实现 MapChangeListener,并在 Intellij Idea (windows) 中运行此示例: // Use Java Collections to crea
在我的 JavaFX 中,我试图拥有一个 ObservableMap和一个 MapChangeListener监听键和值的变化(添加/删除键或相应的值)然后执行其工作。 为了使监听器有效,实现的方法是
我是一名优秀的程序员,十分优秀!