javaswingnetbeansjpaneljgraphx

JGraphX Graph not displayed in JPanel


I am building a Java Application in NetBeans that involves a GUI, through which I am trying to display a graph (representing an automata/finite state machine) using JGraphX. For some reason that I can't figure out, when I run the program, the graph doesn't appear.

I have used JGraphX before in an almost identical way for a different project and it worked perfectly fine then.

Screenshot showing temp GUI with component (automataBuilder1)

Here is the relevant snippet of code representing the component:

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();
        }
    }
}

The update method is called upon a Jbutton being pressed on the GUI

Screenshot of Application running, graph not there!

My testing of the program seems to indicate that my logic is working as it should be. I can get the cells themselves and print out their IDs, the graph just won't display! I've tried a lot of different things I've found online, but all in all my searching has just left me more confused :S. I realise it may be something simple I'm overlooking, but if somebody can point out to me what the problem is I'd appreciate it!


Solution

  • Okay after messing around a bit I realised that I just had to put my automataBuilder container inside another JPanel. No idea why that made it works, but everything is functioning as it should now.