Search code examples
javaswinguser-interfacegraphstream

Add graphstream graph to jpanel with 'intellij.uiDesigner'


I'm trying to add a GraphStreams graph to a JPanel using com.IntelliJ.uiDesigner, and I'm not sure how to initialize the graph's panel without getting the warning

Assignment to UI-bound field will overwrite field generated by UI Designer.
Inspection info: Reports assignments to fields which are bound to components in UI Designer forms.  
Such assignments will cause the component setup code generated by UI Designer for such fields to be ignored.

This is my code:

public class Demo extends JFrame{
    private JPanel graphPanel;
    private JPanel label;

    public Demo(){
        System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer");

        Graph graph = new MultiGraph("Tutorial 1");

        Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
        graphPanel = viewer.addDefaultView(false);  //    <====   this is the line that throws the warning

        ViewerPipe pipeIn = viewer.newViewerPipe();
        pipeIn.addAttributeSink( graph );
        pipeIn.pump();

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

    public static void main(String[] args) {
        demo demo = new demo();

        demo.setContentPane(demo.graphPanel);
        demo.setTitle("Kfir Demo");
        demo.setSize(300, 400);

        demo.setVisible(true);
        demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Solution

  • In order to initialize swing components using com.IntelliJ.uiDesigner one must use the
    method private void createUIComponents().
    You can check out the example here.

    And in this case, this have fixed it.

    public class Demo extends JFrame{
        private JPanel graphPanel;
        private JPanel label;
    
        public Demo(){ }
    
        private void createUIComponents() {
            System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
    
            Graph graph = new MultiGraph("Tutorial 1");
    
            Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
            graphPanel = viewer.addDefaultView(false);
    
            ViewerPipe pipeIn = viewer.newViewerPipe();
            pipeIn.addAttributeSink( graph );
            pipeIn.pump();
    
            graph.addNode("A");
            graph.addNode("B");
            graph.addNode("C");
            graph.addEdge("AB", "A", "B");
            graph.addEdge("BC", "B", "C");
            graph.addEdge("CA", "C", "A");
        }
    
        public static void main(String[] args) {
            demo demo = new demo();
    
            demo.setContentPane(demo.graphPanel);
            demo.setTitle("Kfir Demo");
            demo.setSize(300, 400);
    
            demo.setVisible(true);
            demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }