2013-04-12 29 views
1

我正在使用jgraphx 1.12.0.2,並試圖從代碼中重新排列圖形的頂點。代碼看起來像這樣:使用JGraphX移動單元格

Object[] roots = graph.getChildCells(graph.getDefaultParent(), true, false); 
graph.getModel().beginUpdate(); 
for (int i = 0; i < roots.length; i++) { 
    Object[] root = {roots[i]}; 
    graph.moveCells(root, i * 10 + 5, 50); 
    /* these two lines were added because I thought they might help with the problem */ 
    /* with or without them, the result is the same */ 
    graph.getView().clear(root, true, true); 
    graph.getView().validate(); 
} 
graph.refresh(); 
graph.getModel().endUpdate(); 

問題是,當然,單元不會移動到指定的位置。可能是什麼問題呢?

謝謝!

+0

你的代碼適合我。我唯一的問題,邊緣不能正常移動。 – Marco

回答

0

您不需要刷新,清除或驗證。如果你選擇正確的操作,一切都爲你完成。完全值得讀section 2 of the User Manual,它解釋了核心模型API方法。

在這種情況下,您想要在begin/end update內執行model.setGeometry()。但要確保不使用從getGeometry獲得的幾何對象,必須使用新對象或來自getter的對象克隆。更改模型對象就地打破了撤消模式。

+0

使用setGeometry可能會很棘手,因爲整個座標系也會移動。默認x = 0,y = 0 請看下面的例子: 首先我們使用默認的x = 0,y = 0.插入一個x = 70,y = 70的新頂點, y = 70. 現在我們改變幾何爲x = 50,y = 50. 如果我們添加一個新的頂點,x = 10,y = 10: 新的基準座標是x = 50和y = 50。 在默認條件下(x = 0,y = 0),頂點位於x = 60和y = 60處,因爲x = 50,y = 50是座標的新原點,而不是x = 10,y = 10。所以請記住,以避免混淆。 – Marco