在图形中插入顶点

时间:2015-11-03 14:49:40

标签: java vertex jgraphx

我想在现有图形中添加新顶点。 所以我创建了一个新的单元格,我试图重新连接我的边缘,但我的图形没有更新(边缘)

这是我的代码:

    mxGraph graph = editor.getGraph();

mxCell selectedElt = (mxCell) graph.getSelectionCell();
Object cells[] = { selectedElt };

if (selectedElt.isEdge()) {
    // cell is an edge, so we have source and target
    System.out.println("Source : " + selectedElt.getSource().getId());
    System.out.println("Target : " + selectedElt.getTarget().getId());
} else {
    // edge before
    mxCell beforeEdge = (mxCell) selectedElt.getEdgeAt(0);
    // edge after
    mxCell afterEdge = (mxCell) selectedElt.getEdgeAt(1);
    // moving down the selected cell
    graph.moveCells(cells, 0, 50);

    // create a new vertex
    GraphStyle graphStyle = new GraphStyle(graph);
    mxCell cell = new mxCell("AAM",
            new mxGeometry(selectedElt.getGeometry().getX(), selectedElt.getGeometry().getY(), 80, 50),
            graphStyle.getCalculatorStyleName());
    cell.setVertex(true);

    beforeEdge.setTarget(cell);
    graph.insertEdge(graph.getDefaultParent(), "e33", "", cell, selectedElt);

    graph.addCell(cell);
    graph.repaint();
}

1 个答案:

答案 0 :(得分:0)

而不是致电beforeEdge.setTarget(cell)尝试cell.insertEdge(beforeEdge, false)。这将从前一个顶点移除Edge并将其添加到新顶点。

顺便说一下。我建议将你的代码包装成try-finally块,如下所示:

graph.getModel().beginUpdate();
try {
    // do all the graph related stuff
}
finally {
    graph.getModel().endUpdate();
}