Get a list of all TreeCell objects that currently exist in a TreeView

我的未来我决定 提交于 2019-12-24 14:46:42

问题


I know that TreeCell objects are generated dynamically by the TreeView using a cell factory.

Is there a way to get a list of all TreeCell objects that currently exist?

I suppose I could keep track of them by modifying the cell factory. As in, whenever I create a new cell add it to some list. But then I'm not sure how to remove cells from my list once the TreeView disposes them (because they went out of view).


回答1:


My solution to this is to use weak references:

[A] weak reference is a reference that does not protect the referenced object from collection by a garbage collector[.]

So, add this to your controller:

private final Set<MyTreeCell> myTreeCells = Collections.newSetFromMap(new WeakHashMap<>());

And make your CellFactory look like this:

myTreeView.setCellFactory((treeItem) -> {
        MyTreeCell c = new MyTreeCell(icf);
        Platform.runLater(() -> myTreeCells.add(c));
        return c;
    });

Now, myTreeCells will always contain the currently existing TreeCells.

Update

There is another, quite ugly solution using reflection to get a List of TreeCells. Note that this List is – as far as I know – a snapshot of JavaFXs List of TreeCells at one point in time. It is not backed.

@SuppressWarnings({ "unchecked" })
private Set<MyTreeCell> getListOfTreeCells() {
    try {
        final Field f = VirtualContainerBase.class.getDeclaredField("flow");
        f.setAccessible(true);
        final Field g = VirtualFlow.class.getDeclaredField("cells");
        g.setAccessible(true);
        final Set<MyTreeCell> s = new HashSet<>();
        s.addAll((ArrayLinkedList<MyTreeCell>) g
                .get((f.get((myTreeView.skinProperty().get())))));
        return s;
    }
    catch (NoSuchFieldException | SecurityException | IllegalArgumentException
            | IllegalAccessException e) {
        e.printStackTrace();
    }
    return null;
}



回答2:


treeView.lookupAll(".tree-cell");

This will return a Set of Nodes that can be cast to TreeCells.



来源:https://stackoverflow.com/questions/32335015/get-a-list-of-all-treecell-objects-that-currently-exist-in-a-treeview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!