Search code examples
javajgrapht

JGrapT setting undirected graph


Hello I've been looking in the Internet answer how to make undirected graph in JGraphT but it doesn't work, I have something like this:

g = new ListenableUndirectedGraph<String, MyEdge>(MyEdge.class);
graphAdapter = new JGraphXAdapter<String, MyEdge>(g);

g.addVertex("a");
g.addVertex("b");
g.addEdge("a","b");

layout = new mxOrganicLayout(graphAdapter);
layout.execute(graphAdapter.getDefaultParent());

component = new mxGraphComponent(graphAdapter);

component.setPreferredSize(new Dimension(dim.width - 50, dim.height - 200));

add(component);

Although it is defined as undirected it shows as directed


Solution

  • I have made few manipulations to your code and this should works.

    The part that removes arrows is following part

    // This part to remove arrow from edge
    mxUtils.setCellStyles(graphComponent.getGraph().getModel(),
    cells.toArray(), mxConstants.STYLE_ENDARROW, mxConstants.NONE);
    

    So the full code would be like this, and it is just an example and the rest is left to your fantasy:

    import com.mxgraph.layout.mxCircleLayout;
    import com.mxgraph.model.mxGraphModel;
    import com.mxgraph.swing.mxGraphComponent;
    import com.mxgraph.util.mxConstants;
    import com.mxgraph.util.mxUtils;
    import org.jgrapht.UndirectedGraph;
    import org.jgrapht.ext.JGraphXAdapter;
    import org.jgrapht.graph.DefaultEdge;
    import org.jgrapht.graph.SimpleGraph;
    
    import javax.swing.*;
    import java.awt.*;
    import java.util.Collection;
    
    public class UndirectedGraphClass extends JFrame {
    
        public static void main(String[] args) {
            new UndirectedGraphClass();
        }
    
        private UndirectedGraphClass() {
    
            JGraphXAdapter<String, DefaultEdge> jgxAdapter;
            UndirectedGraph<String, DefaultEdge> g =
                    new SimpleGraph<String, DefaultEdge>(DefaultEdge.class);
    
            g.addVertex("a");
            g.addVertex("b");
            g.addEdge("a", "b");
    
            jgxAdapter = new JGraphXAdapter<String, DefaultEdge>(g);
            mxGraphComponent graphComponent = new mxGraphComponent(jgxAdapter);
            mxGraphModel graphModel = (mxGraphModel) graphComponent.getGraph().getModel();
            Collection<Object> cells = graphModel.getCells().values();
            // This part to remove arrow from edge
            mxUtils.setCellStyles(graphComponent.getGraph().getModel(),
                    cells.toArray(), mxConstants.STYLE_ENDARROW, mxConstants.NONE);
            getContentPane().add(graphComponent);
    
            mxCircleLayout layout = new mxCircleLayout(jgxAdapter);
            layout.execute(jgxAdapter.getDefaultParent());
    
            this.setTitle(" some undirected graph ");
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            this.setPreferredSize(new Dimension(400, 400));
            this.pack();
            this.setVisible(true);
        }
    }
    

    The solution is inspired from this answer.