As I thought, this turns out to be pretty difficult.
There is, by design, no way to get the TreeCell
belonging to a given TreeItem
. This is because of the "virtualization" design of the TreeView
: a minimal number of TreeCell
s are created and these are updated with new TreeItem
s as required. Thus there typically won't be a TreeCell
for every TreeItem
, and the TreeItem
represented by a given TreeCell
may change at any point.
To make this work, first create an observable collection storing all the connections between the trees (e.g. an ObservableSet
should work well). Represent the connections by some class that exposes start and end points which can be used for the lines.
Create custom cell factories for the trees. The cells they return should:
- observe the item they are representing. If the item changes to one that is at an end of one or more connections, then the appropriate point on those connections should be bound to the appropriate transform of the coordinates of the cell.
- If the item changes from one that is at the end of one or more connections, then unbind the appropriate end from the cell coordinates
- observe the observable collection of connections. If one is added for which this cell's item is one end, then bind the coordinates as above
Note that when you bind the coordinates, you need to take into account the fact that the cells may move (e.g. via scrolling or via other changes in GUI layout). You also need to transform the coordinates from the cell's own coordinate system into the coordinate system of whichever pane is holding the connections (obviously, if these are connecting one tree to another, it must be some common scene graph ancestor of both trees).
And finally, you need some housekeeping. The connections need to make sure they either become invisible, or are removed from the scene if they are no longer bound at one or more ends.
I created an example. I just created some simple controls for generating the connections, but you could easily do this with drag and drop instead. The class encapsulating the view of the connection is AssignmentView
; it uses Assignment
to represent the actual data that is connected. The ConnectedTrees
class is the main application and most of the interesting controller-type work is in there. The remaining classes are just data representation. The example is all Java 8; I think it would be much uglier in JavaFX 2.2.