实施房产变更通知的最佳方式是什么? [英] What is the best way to implement notification about property change?
问题描述
有一个bean(类似POJO)并且想要为它的GUI组件制作模型。所以我需要通知GUI组件可以反映的每个属性更改。
A have a bean (POJO-like) and want to make model for GUI component of it. So I need to notify about each property change in order GUI component can reflect.
如何做到这一点?我应该在我的bean中放置通知和监听器存储代码吗?但这会使它变脏。可能会写一些包装?但是这会重复getter和setter。
How to do this? Should I put notification and listener storage code just inside my bean? But this will make it "dirty". May be write some wrapper? But this will duplicate getters and setters.
在Commons或其他地方是否有任何库和/或辅助对象?
Are there any libraries and/or helper objects for this in Commons or somewhere else?
更新
还假设我有 AbstractList< E>
实施。如何快速定性它,即让它通知听众变化?例如,通过触发 ListDataEvent
。我知道我可以实现 AbstractListModel< E>
而不是 AbstractList< E>
但它看起来更糟。我希望保持大多数类似pojo......
Also suppose I have AbstractList<E>
implementation. How to "propertize" it quickly, i.e. make it to notify listeners about changes? For example, by firing ListDataEvent
. I know I can implement AbstractListModel<E>
instead of AbstractList<E>
but it looks worse. I wish to stay mostly "pojo-like"...
推荐答案
看看 FXCollections
(JDK 7):
List<String> list = new ArrayList<String>();
list.add("s1");
list.add("s2");
ObservableList<String> observableList = FXCollections.observableList(list);
observableList.addListener(new ListChangeListener<String>() {
@Override
public void onChanged(Change<? extends String> change) {
while (change.next()) {
if(change.wasAdded()){
System.out.printf("added: %s, from: %d, to: %d%n", change.getAddedSubList(), change.getFrom(), change.getTo());
}else
if(change.wasReplaced()){
System.out.printf("replaced: %s, from: %d, to: %d%n", change.getAddedSubList(), change.getFrom(), change.getTo());
}
}
}
});
observableList.add("s3");
observableList.set(1, "s1-new-value");
这将产生一个输出:
added: [s3], from: 2, to: 3
added: [s1-new-value], from: 1, to: 2
这篇关于实施房产变更通知的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!