我为TreeItems列表创建了一个自定义的ObservableList实现。我的自定义实现可以从我的应用程序内部(使用OSGi EventAdmin)收听各种通知,并相应地更新自己。然后,我希望通过对列表的更改来更新其使用者(TreeView小部件)。但是,我看不出如何通知消费者。
在ObservableList子类中,我正在实现addListener(ListChangeListener),我希望在将对象添加到窗口小部件时调用它。但它永远不会被称为;我没有听众因此没有明显的方式在列表更改时通知任何人。我一定错过了什么。
这是我的TreeItem实现的一个片段,它返回我的ObservableList实例以响应getChildren调用:
@Override
public ObservableList<TreeItem<DataObject>> getChildren() {
if (needChildren) {
needChildren = false;
children = new MyObservableList();
}
return children;
}
这是我的自定义ObservableList实现的删节版本,它只包装FXCollections.observableArrayList并添加一个OSGi事件处理程序。我听内部列表上的更改,以便我可以将这些更改传递给我的听众。
public class MyObservableList implements ObservableList<TreeItem<DataObject>>, EventHandler {
private List<ListChangeListener<? super TreeItem<DataObject>>> changeListeners = new ArrayList<>();
private List<InvalidationListener> invalidationListeners = new ArrayList<>();
private ObservableList<TreeItem<DataObject>> theList;
private int size;
public MyObservableList() {
theList = FXCollections.observableArrayList();
theList.addListener(new ListChangeListener<TreeItem<DataObject>>() {
@Override
public void onChanged(Change<? extends TreeItem<DataObject>> change) {
fireValueChangedEvent(change);
}
});
}
@Override
public int size() {
return theList.size();
}
@Override
public boolean isEmpty() {
return (size == 0);
}
@Override
public boolean contains(Object o) {
return theList.contains(o);
}
@Override
public Iterator iterator() {
return theList.iterator();
}
@Override
public boolean remove(Object o) {
return theList.remove(o);
}
@Override
public boolean addAll(Collection c) {
return theList.addAll(c);
}
@Override
public boolean addAll(int index, Collection c) {
return theList.addAll(index, c);
}
@Override
public void clear() {
theList.clear();
}
@Override
public TreeItem<DataObject> get(int index) {
return theList.get(index);
}
@Override
public int indexOf(Object o) {
return theList.indexOf(o);
}
@Override
public int lastIndexOf(Object o) {
return theList.lastIndexOf(o);
}
@Override
public ListIterator listIterator() {
return theList.listIterator();
}
@Override
public ListIterator listIterator(int index) {
return theList.listIterator(index);
}
@Override
public List<TreeItem<DataObject>> subList(int fromIndex, int toIndex) {
return theList.subList(fromIndex, toIndex);
}
@Override
public Object[] toArray(Object[] a) {
return theList.toArray(a);
}
@Override
public void addListener(ListChangeListener<? super TreeItem<DataObject>> listChangeListener) {
changeListeners.add(listChangeListener);
}
@Override
public void removeListener(ListChangeListener<? super TreeItem<DataObject>> listChangeListener) {
changeListeners.remove(listChangeListener);
}
@Override
public boolean addAll(TreeItem<DataObject>... treeItems) {
return theList.addAll(treeItems);
}
@Override
public boolean setAll(TreeItem<DataObject>... treeItems) {
return theList.setAll(treeItems);
}
@Override
public boolean setAll(Collection<? extends TreeItem<DataObject>> treeItems) {
return theList.setAll(treeItems);
}
@Override
public boolean removeAll(TreeItem<DataObject>... treeItems) {
return theList.removeAll(treeItems);
}
@Override
public boolean retainAll(TreeItem<DataObject>... treeItems) {
return theList.retainAll(treeItems);
}
@Override
public void remove(int i, int i2) {
theList.remove(i, i2);
}
@Override
public Object[] toArray() {
return theList.toArray();
}
@Override
public boolean add(TreeItem<DataObject> dataObjectTreeItem) {
return theList.add(dataObjectTreeItem);
}
@Override
public boolean containsAll(Collection<?> c) {
return theList.containsAll(c);
}
@Override
public boolean removeAll(Collection<?> c) {
return theList.removeAll(c);
}
@Override
public boolean retainAll(Collection<?> c) {
return theList.retainAll(c);
}
@Override
public TreeItem<DataObject> set(int index, TreeItem<DataObject> element) {
return theList.set(index, element);
}
@Override
public void add(int index, TreeItem<DataObject> element) {
theList.add(index, element);
}
@Override
public TreeItem<DataObject> remove(int index) {
return theList.remove(index);
}
@Override
public void addListener(InvalidationListener invalidationListener) {
invalidationListeners.add(invalidationListener);
}
@Override
public void removeListener(InvalidationListener invalidationListener) {
invalidationListeners.remove(invalidationListener);
}
private void fireValueChangedEvent(ListChangeListener.Change<? extends TreeItem<DataObject>> change) {
for (ListChangeListener<? super TreeItem<DataObject>> listener : changeListeners) {
listener.onChanged(change);
}
}
@Override
public void handleEvent(Event event) {
// Here I add or remove TreeItem<DataObject> instances to the list based on event.
//
// At this point, onChanged() gets called above in my listener, but my changeListeners list is empty. There is
// no one to pass the Change on to.
}
}
感谢您的帮助。
答案 0 :(得分:1)
在查看JavaFX源代码后,我想出了这里发生了什么。
事实证明,监听子项的ObservableList都是在TreeItem本身中设置的,而不是我以某种方式假设的TreeView。这意味着覆盖getChildren()的TreeView的任何子类必须调用super.getChildren()并将其子项添加到结果列表中。这意味着使用自定义ObservableList实现是不可能的,因为TreeItem被硬编码以使用FXCollections.observableArrayList()来创建列表。
我正在采取不同的方法,现在我调用super.getChildren(),添加我的孩子,然后实例化另一个对象,该对象包含对该列表的引用,并执行我的所有应用程序的事件处理业务,根据需要列出。所以我的getChildren()方法现在看起来像这样。
private MyEventHandler eventHandler;
private ObservableList<TreeItem<DataObject>> children;
@Override
public ObservableList<TreeItem<DataObject>> getChildren() {
if (needChildren) {
needChildren = false;
children = super.getChildren();
eventHandler = new MyEventHandler(children); // handles app events
}
return children;
}