2

I'm doing the vertex insertion as follows:

String id = "1";
String value = "One";
Object v = graph.insertVertex(parent, id, value, 30, 30, 0, 0);

So I need to get this vertex by id...

Object[] cells = graph.getChildVertices(graph.getDefaultParent());
for (Object c : cells)
{
    mxCell cell = (mxCell) c;
    System.out.println("id: " + cell.getId() + ", value: " + cell.getValue());
}
//result:
//id: 2, value: One

Why the id has changed? How could I get the vertex properly?

JulianoMartins
  • 543
  • 1
  • 8
  • 19

2 Answers2

3

Try mxGraphModel.getCell(String id).

Frodo Baggins
  • 8,290
  • 6
  • 45
  • 55
user1084282
  • 1,003
  • 7
  • 6
  • While this answer may be correct, please add some explanation. Imparting the underlying logic is more important than just giving the code, because it helps the OP and other readers fix this and similar issues themselves. – CodeMouse92 Oct 10 '15 at 01:06
0

When you create a new mxGraph without a specific mxGraphModel, the model automatically creates two nodes.
The "root" node using the id = "0" and another node as a child of the root node with the id = "1". This node is the node you normally get when using graph.getDefaultParent().
So when you try to insert a node using the id = "1" the model already contains a node with the given id, so it creates a new id and inserts the node. Try a different id and it should work fine.

F. Lumnitz
  • 688
  • 4
  • 11
  • 1
    Just to add extra to this correct answer, we had similar problems on our project, I found the BEST way to handle the scenario was to use alpha ID's like "Label1", "Label2" then that way I NEVER conflicted with anything the library auto generated. – shawty Feb 04 '16 at 16:38