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).
My solution to this is to use weak references:
So, add this to your controller:
And make your CellFactory look like this:
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.