[英]JGraphX Graph not displayed in JPanel
我正在NetBeans中構建一個涉及GUI的Java應用程序,我試圖通過該應用程序使用JGraphX顯示圖形(代表自動機/有限狀態機)。 由於某種原因,我無法弄清楚,當我運行該程序時,該圖沒有出現。
以前,我以幾乎相同的方式將JGraphX用於不同的項目,然后效果很好。
該屏幕快照顯示了帶有組件(automataBuilder1)的臨時GUI
這是代表組件的相關代碼片段:
import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxGraphModel;
import com.mxgraph.swing.handler.mxGraphHandler;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.swing.util.mxMorphing;
import com.mxgraph.util.mxEvent;
import com.mxgraph.util.mxEventObject;
import com.mxgraph.util.mxEventSource.mxIEventListener;
import com.mxgraph.view.mxGraph;
import java.awt.BorderLayout;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
//Some imports may be unnecessary at the moment.
public class AutomataBuilder extends JPanel {
private final mxGraph graph;
private final mxGraphComponent graphComp;
private final mxGraphHandler graphHand;
private mxGraphModel model;
private Automata automata;
//constructor
public AutomataBuilder(){
graph = new mxGraph();
graphComp = new mxGraphComponent(graph);
graphHand = new mxGraphHandler(graphComp);
automata = new Automata(); //The automata the graph represents
graphComp.setBorder(null);
graphComp.setEnabled(false);
graphComp.setToolTips(true);
this.setLayout(new BorderLayout());
this.add(graphComp);
}
.
. //other methods, not relevant here.
.
public void update(){
Object parent = graph.getDefaultParent();
//Graph already represented on the backend, can get nodes and edges from this
HashMap nodes = getAllNodes(); //Returns all nodes for graph
HashSet edges = getAllEdges(); //Returns all edges for graph
//begin model update
graph.getModel().beginUpdate();
try
{
//iterate through each node and add to graph using insertVertex
Iterator it = nodes.entrySet().iterator();
while(it.hasNext()){
Map.Entry pair = (Map.Entry)it.next();
graph.insertVertex(parent, (String)pair.getKey(), pair.getValue(), 0, 0, 15, 15);
}
//iterate through each edge and add to graph using insertVertex
it = edges.iterator();
while(it.hasNext()){
Edge current = (Edge)it.next();
graph.insertEdge(parent, current.printEdge(), current, current.getFrom(), current.getTo());
}
}
finally
{
graph.getModel().endUpdate();
}
}
}
在GUI上按下Jbutton時,將調用update方法
我對該程序的測試似乎表明我的邏輯正在按預期的方式工作。 我可以自己獲取單元格並打印出其ID,該圖將不會顯示! 我已經嘗試了很多在網上找到的不同方法,但是所有的搜索都使我更加困惑:S。 我意識到這可能是我忽略的簡單事情,但是如果有人可以向我指出問題所在,我將不勝感激!
好吧,經過一番混亂之后,我意識到我只需要將automataBuilder容器放入另一個JPanel中即可。 不知道為什么會這樣,但是現在一切都在運轉。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.