如何监视ObservableList JavaFX中包含的对象的更改 [英] How to monitor changes on objects contained in an ObservableList JavaFX
问题描述
我真的很难理解 ObservableList
对象在JavaFX中工作。我想监视是否修改了 List
中的对象。到目前为止,我只看到我可以监视 List
,作为一个实体本身,是否已被修改......但不是中的对象列表
:
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<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.
谢谢
推荐答案
如果要监视列表中对象的更改而不是列表本身,则必须将侦听器附加到列表的对象而不是到列表。
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.
当然,为了能够做到这一点,对象必须支持这一点。 java.lang.Object
不支持此功能。
Of course to be able to do this, the objects must support this. java.lang.Object
does not support this.
请看一下 ObservableValue
界面。实现此接口的对象支持您正在寻找的这种监视。 ObservableValue
的javadoc页面列出了实现此接口的所有JavaFX内置类(列表非常looooong)。
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).
您必须使用其中任何一个,或者您必须自己实现界面。并将更改侦听器添加到对象而不是列表中。
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.
这篇关于如何监视ObservableList JavaFX中包含的对象的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!