如何在添加顶点后更新JGraphT视图?

时间:2017-05-28 19:00:27

标签: java jgraphx

我使用JGraphT库创建了一个图表:

@Override
public void actionPerformed(ActionEvent e) {
    String a="1";
    String b="2";
    g.addVertex(a);
    g.addVertex(b);
    g.addEdge(a,b);

}

public static class MyEdge extends DefaultWeightedEdge { //weight
    @Override
    public String toString() {
        return String.valueOf(getWeight());
    }
}

我想在按下按钮后动态添加新顶点

IPersonRepository

如何刷新视图?

2 个答案:

答案 0 :(得分:1)

试试这个:

public void actionPerformed(ActionEvent e) {
    String a="1";
    String b="2";
    [your mxGraph variable].getModel().beginUpdate();
    g.addVertex(a);
    g.addVertex(b);
    g.addEdge(a,b);
    [your mxGraph variable].getModel().endUpdate();
    [your mxGraph variable].refresh();

}

Java API for mxGraph
Java API for mxIGraphModel

答案 1 :(得分:0)

TobiasH它没有用,但是我尝试过这样的事情并且运作正常

 if (e.getSource() == add) {
        String a = vertex.getText().toString();
        if (a != null && !a.equals("")) {
            g.addVertex(a);

            vertexSet.add(a);

            if (checked) {
                String b = vertex1.getText().toString();

                vertexSet.add(b);
                edges.add(new Edge(a, b)); // zbiór krawędzi


                g.addVertex(b);
                g.addEdge(a, b);
            }
            layout.execute(graphAdapter.getDefaultParent());
        }
    }