JGraphX 图形未显示在 JPanel 中

JGraphX Graph not displayed in JPanel

我正在 NetBeans 中构建一个涉及 GUI 的 Java 应用程序,我试图通过它使用 JGraphX 显示图形(代表 automata/finite 状态机)。由于某些我无法弄清楚的原因,当我 运行 程序时,图表没有出现。

我以前在一个不同的项目中以几乎相同的方式使用过 JGraphX,然后它工作得非常好。

Screenshot showing temp GUI with component (automataBuilder1)

这里是代表组件的相关代码片段:

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 时调用更新方法

Screenshot of Application running, graph not there!

我对程序的测试似乎表明我的逻辑正常工作。我可以自己获取细胞并打印出它们的 ID,但图表不会显示!我已经尝试了很多我在网上找到的不同的东西,但总的来说,我的搜索让我更加困惑:S。我意识到这可能是我忽略的简单问题,但如果有人能向我指出问题所在,我将不胜感激!

好吧,经过一番折腾后,我意识到我只需要将我的 automataBuilder 容器放入另一个 JPanel 中。不知道为什么会这样,但现在一切正常。