Class: PrepopulatedTutorial
This example shows three alternatives to display a prepopulated graph model in a window. Prepopulated means that there are already nodes in the model before the model was placed in a GraphPane. There was a problem in the initial release which made this break. That problem has been fixed and it will work now, except for one little gotcha, which is if you set the model while the window is closed, the GraphPane thinks its size is 0x0, so the nodes all get layed out in the upper-left corner of the canvas. The way to fix this is to either set the model once the window is open, or explicitly call a global layout once the window has been opened. More comments below in the individual methods.
This is the first thing you'd probably think of, but this happens to be bogus, because the incremental layout is applied to the nodes before the window is showing, meaning that the nodes are layed out in a 0x0 frame, and are all clustered in the upper-left corner. This is remedied in the other techniques given below.
TutorialWindow f = new TutorialWindow("Bogus Layout"); f.getContentPane().add("Center", new JGraph(model)); f.setLocation(0, 0); f.setSize(400, 300); f.setVisible(true);
In this version you construct the graph widget with the model, and apply a layout to the graph once the window is showing. I think the "set model post display" version is preferable, but this might be useful in some cases.
TutorialWindow f = new TutorialWindow("Layout Post Display"); JGraph g = new JGraph(model); f.getContentPane().add("Center", g); f.setSize(400, 300); f.setLocation(200, 200); f.setVisible(true); (new RandomLayout()).layout(g.getGraphPane().getGraphView(), model.getGraph());
In this version you construct the graph widget with the default constructor (giving it an empty graph), and then set the model once the window is showing.
TutorialWindow f = new TutorialWindow("Set Model Post Display"); JGraph g = new JGraph(); f.getContentPane().add("Center", g); f.setSize(400, 300); f.setLocation(100, 100); f.setVisible(true); g.setGraphModel(model);