'GraphStream & Swing - Can't display my graph correctly

I'm a beginner with GraphStream and Swing, I would like to insert a Graph in a JPanel, but while trying I get the following display:

Main Frame

I added a graph.display() in the code to see if the problem came from the graph but it seems to display correctly:

The graph with graph.display()

I have the impression that when the graph is in the panel the vertices do not want to position themselves correctly.

Code:

My custom panel:

import org.graphstream.graph.implementations.SingleGraph;
import org.graphstream.ui.view.View;
import org.graphstream.ui.view.Viewer;
import javax.swing.*;
import java.awt.*;

public class PanelGraph extends JPanel {

    public SingleGraph graph = new SingleGraph("Test graph");

    public PanelGraph() {

        graph.addNode("A");
        graph.addNode("B");
        graph.addNode("C");
        graph.addEdge("AB", "A", "B");
        graph.addEdge("BC", "B", "C");
        graph.addEdge("CA", "C", "A");

        graph.setStrict(false);
        graph.setAutoCreate(true);

        Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_GUI_THREAD);
        View view = viewer.addDefaultView(false);
        setLayout(new BorderLayout());
        add((Component) view, BorderLayout.CENTER);
    }
}

My frame:

import javax.swing.*;
import java.awt.*;

public class TestFrame extends JFrame {

    public TestFrame() {
        super("Test frame");
        super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        init();
    }

    private void init() {
        JPanel panelInputs = new JPanel();
        JPanel panelSide = new JPanel();
        PanelGraph panelGraph = new PanelGraph(); // The graph is in this panel

        panelInputs.add(new JLabel("Inputs panel"));
        panelSide.add(new JLabel("Side panel"));

        this.getContentPane().setLayout(new GridBagLayout());
        this.getContentPane().add(panelInputs, new GridBagConstraints(0, 0, 3, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
        this.getContentPane().add(panelSide, new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
        this.getContentPane().add(panelGraph, new GridBagConstraints(1, 1, 2, 1, 2, 2, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

        this.setPreferredSize(new Dimension(1600, 900));
        this.pack();
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        TestFrame frame = new TestFrame();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

EDIT : I found the solution on the documentation of graphstream (https://graphstream-project.org/doc/Tutorials/Graph-Visualisation/1.0/) in the Automatic layout section, when using a viewer you have to add viewer.enableAutoLayout(); to display it correctly

New code with the solution:

My custom panel:

import org.graphstream.graph.implementations.SingleGraph;
import org.graphstream.ui.view.View;
import org.graphstream.ui.view.Viewer;
import javax.swing.*;
import java.awt.*;

public class PanelGraph extends JPanel {

    public SingleGraph graph = new SingleGraph("Test graph");

    public PanelGraph() {

        graph.addNode("A");
        graph.addNode("B");
        graph.addNode("C");
        graph.addEdge("AB", "A", "B");
        graph.addEdge("BC", "B", "C");
        graph.addEdge("CA", "C", "A");

        graph.setStrict(false);
        graph.setAutoCreate(true);

        Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_GUI_THREAD);
        viewer.enableAutoLayout();
        View view = viewer.addDefaultView(false);
        setLayout(new BorderLayout());
        add((Component) view, BorderLayout.CENTER);
    }
}


Solution 1:[1]

I found the solution on the documentation of graphstream (https://graphstream-project.org/doc/Tutorials/Graph-Visualisation/1.0/) in the Automatic layout section, when using a viewer you have to add viewer.enableAutoLayout(); to display it correctly

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BenoitPEGAZ