2017-05-28 72 views
2

我創建使用JGraphT庫圖:如何在添加頂點後更新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()); 
    } 
} 

我怎樣才能刷新視圖後動態添加一個新的頂點?

回答

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

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()); 
     } 
    } 
相關問題