1

I'm using jgraphx 1.12.0.2 and I'm trying to rearrange the vertices of a graph from code. The code looks something like this:

Object[] roots = graph.getChildCells(graph.getDefaultParent(), true, false);
graph.getModel().beginUpdate();
for (int i = 0; i < roots.length; i++) {
    Object[] root = {roots[i]};
    graph.moveCells(root, i * 10 + 5, 50);
    /* these two lines were added because I thought they might help with the problem */
    /* with or without them, the result is the same */
    graph.getView().clear(root, true, true);
    graph.getView().validate();
 }
 graph.refresh();
 graph.getModel().endUpdate();

The problem is, of course, that the cells don't move to the indicated positions. What could be the problem?

Thanks!

alex_and_ra
  • 219
  • 1
  • 16

1 Answers1

0

You don't need the refresh, the clear, or the validate. If you pick the correct operation, everything is done for you. It's worth reading section 2 of the User Manual fully, it explains the core model API methods.

In this case you want to perform a model.setGeometry() within the begin/end update. But make sure you don't use the geometry object obtained from getGeometry, you must use either a new object, or a clone of the object from the getter. Changing model objects in-place breaks the undo model.

Frodo Baggins
  • 8,290
  • 6
  • 45
  • 55
  • Using setGeometry can be tricky, because the entire coordinate system will also moved. Default x = 0, y = 0. See the following example: At first we use the default x = 0, y = 0. Inserting a new vertex with x = 70, y = 70 results obviously in x = 70, y = 70. Now we change the geometry to x = 50, y = 50. If we add a new vertex with x = 10, y = 10: The new base coordinates are x = 50 and y = 50. In default terms (x = 0, y = 0) the vertex is at x = 60 and y = 60, because x = 50, y = 50 are the new origin of coordinates and not at x = 10, y = 10. So keep that in mind to avoid confusion. – Marco Jan 27 '16 at 19:05