The TreeLayout does a 1 time rendering of the vertexes of a tree. So every vertex that is in the tree the first time it is rendered is placed okay.
However, childs that are added later on ARE rendered but NOT at the correct place. Rather, the layout should re-calculate the placing of all vertexes. (Or alternatively only the newly added child.)
A simple demo is attahed showing the problem. (New nodes get added every 2 seconds, which are then incorrectly placed.)
Test app showing the bug
I'm facing the same problem.
Looking at http://jung.cvs.sourceforge.net/viewvc/jung/jung2/jung-algorithms/src/main/java/edu/uci/ics/jung/algorithms/layout/TreeLayout.java?revision=1.19&view=markup, I get the impression that the positions of the vertices are calculated in the protected method buildTree().
It would be useful to have at least a public method triggering the recalculation of the vertex positions on the display.
As I workaround, I wrote a small class inheriting from TreeLayout which implements the following method:
public void updateLayout()
{
this.alreadyDone.clear();
this.buildTree();
}
Not sure whether this 100% correct, but it seems to work.
Caveats:
- I have to call this method by hand after modifying the tree to be displayed
- the positions of previously existing nodes (e.g. the root) on the display move with respect to the situation before adding/removing edges/vertices.
Obviously, it would be nicer if the layout would listen to changes of the tree content and update itself...