I am implementing drag and drag between two tree views. When a treeItem is dropped onto another treeView of treeItem a line connection is established between the two treeItems. This is working fine , but to have a connection initially without a drag and drop events is problem to me. I am using treeCell for the drag and drop events.
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
TreeCells are created and these are updated with new
TreeItems 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:
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.
Set<Node> treeCells = tree.lookupAll(".tree-cell"); List<Node> cells = new ArrayList<>(treeCells); int row = tree.getRow(((TreeItem) treeItem)); TreeCell cell= ((TreeCell) cells.get(row))