Get the treeCell Implementation using the treeItem in JavaFX


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.

5/6/2014 9:21:25 AM

Accepted Answer

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:

  1. 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.
  2. 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
  3. 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.

5/15/2014 6:53:46 PM

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))

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow