java 如何监视 ObservableList JavaFX 中包含的对象的更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26838183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to monitor changes on objects contained in an ObservableList JavaFX
提问by BlackLabrador
I really have difficulties to understand how the ObservableList
object is working in JavaFX. I want to monitor if an object in the List
has been modified. So far, I only see that I can monitor if the List
, as an entity itself, has been modified... but not the objects within the List
:
我真的很难理解ObservableList
对象在 JavaFX 中是如何工作的。我想监视 中的对象List
是否已被修改。到目前为止,我只看到我可以监视List
作为实体本身的 是否已被修改......但不能监视 中的对象List
:
ObservableList<Stuff> myList = FXCollections.<Stuff>observableArrayList();
myList.add(someStuff);
myList.addListener((ListChangeListener.Change<? extends Stuff> change) -> {
while(change.next()){
if(change.wasUpdated()){
System.out.println("Update detected");
}
else if(change.wasPermutated()){
}
else{
for (Stuff remitem : change.getRemoved()) {
//do things
}
for (Stuff additem : change.getAddedSubList()) {
//do things
}
}
}
});
someStuff.setThing("clobber"); // trigger listener
Is there a way to do this? I'm looking for a workflow such that a modification to the object triggers → modification on the list → refresh on a some view.
有没有办法做到这一点?我正在寻找一种工作流程,以便对对象的修改触发 → 列表上的修改 → 在某个视图上刷新。
Thanks
谢谢
回答by icza
If you want to monitor changes of the objects inside the list instead of the list itself, then you have to attach listeners to the objects of the list and not to the list.
如果要监视列表内对象的更改而不是列表本身,则必须将侦听器附加到列表的对象而不是列表。
Of course to be able to do this, the objects must support this. java.lang.Object
does not support this.
当然,为了能够做到这一点,对象必须支持这一点。java.lang.Object
不支持这个。
Instead take a look at the ObservableValue
interface. Objects that implement this interface support this kind of monitoring you're looking for. The javadoc page of ObservableValue
lists all the JavaFX built-in classes that implement this interface (the list is quite looooong).
而是看一下ObservableValue
界面。实现此接口的对象支持您正在寻找的这种监视。的 javadoc 页面ObservableValue
列出了实现此接口的所有 JavaFX 内置类(列表很长)。
Either you have to use any of those, or you have to implement the interface yourself. And add your change listeners to the objects and not to the list.
您要么必须使用其中任何一个,要么必须自己实现接口。并将您的更改侦听器添加到对象而不是列表中。
回答by Lealo
public static void main(String[] args) {
ArrayList<Integer> intList = new ArrayList();
intList.add(0);
intList.add(1);
ObservableList<Integer> ob = FXCollections.observableArrayList(intList);
ob.addListener(new ListChangeListener<Integer>() {
@Override
public void onChanged(javafx.collections.ListChangeListener.Change<? extends Integer> c) {
System.out.println("Changed on " + c);
if(c.next()){
System.out.println(c.getFrom());
}
}
});
ob.set(0, 1);
}
The event (c in my case) is the index that the change occurred on (when you do .getFrom()). Also if you print the event out you get a string that tells you exactly what happend. You mistakenly interpret it has a change being done on the list as a whole!
事件(在我的例子中是 c)是发生变化的索引(当你执行 .getFrom() 时)。此外,如果您将事件打印出来,您会得到一个字符串,告诉您究竟发生了什么。你错误地理解它在整个列表上做了一个改变!