gpt4 book ai didi

java - 添加顶点后如何更新 JGraphT View ?

转载 作者:行者123 更新时间:2023-11-30 10:29:07 24 4
gpt4 key购买 nike

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

g = new ListenableUndirectedWeightedGraph <String, MyEdge>(MyEdge.class);
graphAdapter = new JGraphXAdapter<String, MyEdge>(g);

layout = new mxOrganicLayout(graphAdapter);
layout.execute(graphAdapter.getDefaultParent());

mxGraphComponent component = new mxGraphComponent(graphAdapter);

component.setPreferredSize(new Dimension(dim.width - 50, dim.height - 200));

add(component);

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

@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());
}
}

如何刷新 View ?

最佳答案

试试这个:

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

关于java - 添加顶点后如何更新 JGraphT View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44230955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com