2

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?

charlie123
  • 253
  • 1
  • 5
  • 17
  • I believe the reason they are out of order is because JUNG's `TreeLayout` stores the vertices in a HashMap (where order is not guaranteed). I believe it would work if you copied JUNG's `TreeLayout` code and replaced it with `TreeHashMaps`. – sdasdadas Jul 02 '13 at 21:38
  • sdasdadas, that's incorrect. See below. – Joshua O'Madadhain Jul 17 '13 at 06:02

1 Answers1

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.

Joshua O'Madadhain
  • 2,704
  • 1
  • 14
  • 18
  • I'm not quite sure I understand why `setGraph` wouldn't update the `TreeLayout`. They both assign the field `graph` and they both call `buildTree()`. The only thing the constructor does that the `setGraph` method does not do is set the spacings. – sdasdadas Jul 23 '13 at 22:56
  • I've looked a bit further and you are right. It's because `TreeLayout` maintains a `Set` of vertices that have already been positioned. If you have a second, do you mind explaining why the layout needs that `Set`? – sdasdadas Jul 23 '13 at 23:51