dimanche 31 janvier 2016

JavaFX TreeView: Access list of TreeCells

I am trying to get a list of the currently existing TreeCells out of my TreeView. My current attempt looks like this:

private Set<MyTreeCell> getMyTreeCells(){
    try {
        @SuppressWarnings("unchecked")
        Field f = ((VirtualContainerBase<TreeView<myNode>, TreeViewBehavior<myNode>, MyTreeCell>) myTreeView.skinProperty().get()).getClass().getDeclaredField("flow");
        f.setAccessible(true);
        @SuppressWarnings("unchecked")
        Field g = ((VirtualFlow<MyTreeCell>) f.get(((VirtualContainerBase<TreeView<myNode>, TreeViewBehavior<myNode>, ProofTreeCell>) proofTreeView.skinProperty().get()))).getClass().getDeclaredField("cells");
        g.setAccessible(true);
        @SuppressWarnings("unchecked")
        ArrayLinkedList<MyTreeCell> l = (ArrayLinkedList<MyTreeCell>) g.get(((VirtualFlow<MyTreeCell>) f.get(((TreeViewSkin<myNode>) myTreeView.skinProperty().get()))));
        Set<myTreeCell> s = new HashSet<>();
        s.addAll(l);
        return s;
    }
    catch (NoSuchFieldException e) {
        e.printStackTrace();
    }
    catch (SecurityException e) {
        e.printStackTrace();
    }
    catch (IllegalArgumentException e) {
        e.printStackTrace();
    }
    catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return null;
}

Unfortunately it throws java.lang.NoSuchFieldException: flow. But why? Clearly the Field flow exists in the Class VirtualContainerBase.

Please note that I only wrote this for fun, and not to actually be used in productive code. I already have a cleaner solution for the problem.





Aucun commentaire:

Enregistrer un commentaire