2014-06-24 108 views
0

我正在研究JGraphX。我使用graphComponent的insertEdge方法在兩個頂點之間添加了一條邊。但邊緣不可見。我可以看到邊緣,如果我保存,然後關閉,然後打開相同的文件。 爲什麼我添加時看不到?如果我爲同一個單元格創建一個邊緣,即graph.insertEdge(parent,null,「hasEntity」,newCell,newCell),那麼它將自己添加邊緣,即源和目標頂點相同。我不需要關閉並在這裏打開圖表。邊緣在JGraphX圖中不可見

mxCell defaultActivityEdge = (mxCell) graph.insertEdge(parentObject, null, "hasActivity", cell3, newCell); 
graph.addCell(defaultActivityEdge); 
defaultActivityEdge.setVisible(true); 
graph.clearSelection(); 
graph.refresh() 
+0

顯示你的代碼PLS或代碼的樣本。 – AJJ

+0

除非我們看到你的邏輯的一部分,否則你不會從這裏得到幫助。 – AJJ

+0

mxCell defaultActivityEdge =(mxCell)graph.insertEdge(parentObject,null,「hasActivity」,cell3,newCell); graph.addCell(defaultActivityEdge); defaultActivityEdge.setVisible(true); graph.clearSelection(); graph.refresh(); – Pramod

回答

1

您不需要將insertEdge()的返回對象作爲單元添加到圖中。請查看下面的示例並在您的代碼中進行修改。

public class MxGraphSample { 

    public static void creategraph() { 

     final JFrame frame = new JFrame(); 
     frame.setSize(300, 300); 
     JPanel panel = new JPanel(); 
     panel.setSize(frame.getMaximumSize().width, 
       frame.getMaximumSize().height); 

     final mxGraph graph = new mxGraph(); 
     Object parent = graph.getDefaultParent(); 
     graph.getModel().beginUpdate(); 

     try { 
      Object v1 = graph.insertVertex(parent, null, "v1", 20, 20, 80, 30); 
      Object v2 = graph.insertVertex(parent, null, "v2", 120, 70, 80, 30); 
      Object v3 = graph.insertVertex(parent, null, "v3", 220, 70, 80, 30, 
        "fillColor=lightgreen"); 

      graph.insertEdge(parent, null, "", v1, v2); 
      graph.insertEdge(parent, null, "", v1, v3, "strokeColor=lightgreen"); 

      graph.cellsFolded(new Object[] {v1, v2, v3}, true, true); 

      mxCompactTreeLayout layout = new mxCompactTreeLayout(graph); 
      layout.setLevelDistance(40); 
      layout.setNodeDistance(30); 
      layout.setEdgeRouting(false); 
      layout.setUseBoundingBox(false); 
      layout.execute(graph.getDefaultParent()); 

     } finally { 
      graph.getModel().endUpdate(); 
     } 
     final mxGraphComponent graphComponent = new mxGraphComponent(graph); 

     graphComponent.setFoldingEnabled(true); 

     panel.setLayout(new BorderLayout()); 
     panel.add(graphComponent, BorderLayout.CENTER); 

     frame.add(panel); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

    public static void main(String[] args) { 
     creategraph(); 
    } 
} 

輸出

enter image description here