tags:

views:

42

answers:

1

Is it possible to display a JGraphX by adding it to a JLable? Much of testJGraphX (below) is taken from the JGraphX Hello World example, but the graph is not displayed in jLable1. Is there a better container than JLabel for a JGraphX?

public class TestJGraphX extends javax.swing.JFrame implements TableModelListener {
    public TestJGraphX() {
        initComponents();
        testJGraphX();
    }
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
  ...
  .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE)
  ...
  .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE))
  ...
 }
    private void testJGraphX() {
        mxGraph graph = new mxGraph();
        Object parent = graph.getDefaultParent();
        graph.getModel().beginUpdate();
        try {
            Object v1 = graph.insertVertex(parent, null, "Hello", 20, 20, 80, 30);
            Object v2 = graph.insertVertex(parent, null, "World!", 240, 150, 80, 30);
            graph.insertEdge(parent, null, "Edge", v1, v2);
        } finally {
            graph.getModel().endUpdate();
        }
        mxGraphComponent graphComponent = new mxGraphComponent(graph);
        jLabel1.add(graphComponent);
    }
}
+1  A: 

A JPanel should work, or indeed just add it straight to a JFrame.

Edit - the code below displays the graph fine for me.

import javax.swing.JFrame;

import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;

public class GraphFrame extends JFrame {
    public static void main(String[] args) {
        mxGraph graph = new mxGraph();
        Object parent = graph.getDefaultParent();
        graph.getModel().beginUpdate();
        try {
            Object v1 = graph.insertVertex(parent, null, "Hello", 20, 20, 80,
                    30);
            Object v2 = graph.insertVertex(parent, null, "World!", 240, 150,
                    80, 30);
            graph.insertEdge(parent, null, "Edge", v1, v2);
        } finally {
            graph.getModel().endUpdate();
        }
        mxGraphComponent graphComponent = new mxGraphComponent(graph);

        GraphFrame frame = new GraphFrame();
        frame.add(graphComponent);
        frame.pack();
        frame.setVisible(true);
    }
}
William
I replaced the JLabel with a JPanel but the graph is still not displayed.
JackN
Try just adding it straight to the JFrame, i.e. add(graphComponent); pack();
William
Replaced jPanel1.add(graphComponent); with add(graphComponent); pack(); ... Still no luck.
JackN
Just tried myself (see above) and it seems to work fine..
William
Yes. That worked. Thanks.
JackN