JUNG Tree Layout dynamic update

2019-07-21 09:43发布

问题:

I'm using the TreeLayout in JUNG for my graph. The user is able to make changes to the graph and it should then update and show the changes.

To enable a update, I create the graph again and set the layout using it:

graph = new DelegateForest<String, Integer>(
          new DirectedOrderedSparseMultigraph<String, Integer>());
createGraph()    //adds the vertices and edges to the graph
layout.setGraph(graph);
vv.repaint();

This does update the tree to what it should be, but the tree layout is lost.

Most of the nodes do appear in the right place, but the leaf nodes appear in random places. Is it possible to get back the tree structure that you get the first time you create the graph?

回答1:

You need to create a new TreeLayout instance with the updated graph, and update the visualization with the new TreeLayout instance. You're getting the new nodes in random places because the TreeLayout instance you have hasn't calculated positions for them.

This could be made more efficient for some types of graph updates (additions only). But it's not high on our list right now.