I need an org chart tree and I want to be able to collapse and expand the nodes at any level. I am new to JGraphX but from what I have read it sounds like the way to implement folding is to group the vertexes. The problem is when I create the group it puts all the child vertices inside the parent vertex.
Here is some example code that gives a great layout but does not support folding:
package com.mxgraph.examples.swing;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.SwingConstants;
import com.mxgraph.layout.mxCompactTreeLayout;
import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
import com.mxgraph.model.mxGeometry;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxPoint;
import com.mxgraph.util.mxRectangle;
import com.mxgraph.view.mxGraph;
import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
public class HelloWorld extends JFrame
{
/**
*
*/
private static final long serialVersionUID = -2707712944901661771L;
public HelloWorld()
{
super("Hello, puppies!");
mxGraph graph = new mxGraph();
Object parent = graph.getDefaultParent();
graph.getModel().beginUpdate();
try
{
//Notice that the parent is the default parent...
//The hierarchical structure looks great but I cannot collapse/expand the tree.
Object vDogsRoot = graph.insertVertex(parent, null, "DOG", 0, 0, 80, 30);
Object v2 = graph.insertVertex(parent, null, "Shar Pei", 0, 0, 80, 30);
Object v3 = graph.insertVertex(parent, null, "Pug", 0, 0, 80, 30);
Object v4 = graph.insertVertex(parent, null, "Cocker Spaniel", 0, 0, 80, 30);
Object v5 = graph.insertVertex(parent, null, "Pit Bull", 0, 0, 80, 30);
Object v6 = graph.insertVertex(parent, null, "Chihuahua", 0, 0, 80, 30);
graph.insertEdge(parent, null, "", vDogsRoot, v2);
graph.insertEdge(parent, null, "", vDogsRoot, v3);
graph.insertEdge(parent, null, "", vDogsRoot, v4);
graph.insertEdge(parent, null, "", vDogsRoot, v5);
graph.insertEdge(parent, null, "", vDogsRoot, v6);
mxHierarchicalLayout layout = new mxHierarchicalLayout(graph);
layout.setUseBoundingBox(false);
layout.execute(parent);
}
finally
{
graph.getModel().endUpdate();
}
mxGraphComponent graphComponent = new mxGraphComponent(graph);
getContentPane().add(graphComponent);
}
public static void main(String[] args)
{
HelloWorld frame = new HelloWorld();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 320);
frame.setVisible(true);
}
}
Produces:
Great start but no collapse button. The following code demonstrates the problem I am having. To support folding I attempt to create a group by changing the parent of the vertices from the default parent to the vDogVertex which is the root of the tree. This becomes collapsible, however all of the child vertices are inside of the vDogVertex and this ruins the trees layout.
package com.mxgraph.examples.swing;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.SwingConstants;
import com.mxgraph.layout.mxCompactTreeLayout;
import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
import com.mxgraph.model.mxGeometry;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxPoint;
import com.mxgraph.util.mxRectangle;
import com.mxgraph.view.mxGraph;
import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
public class HelloWorld extends JFrame
{
/**
*
*/
private static final long serialVersionUID = -2707712944901661771L;
public HelloWorld()
{
super("Hello, puppies!");
mxGraph graph = new mxGraph();
Object parent = graph.getDefaultParent();
graph.getModel().beginUpdate();
try
{
//Notice this time the parent is the vDogsRoot vertex.
//This creates a cell group if I understand correctly.
Object vDogsRoot = graph.insertVertex(parent, null, "DOG", 0, 0, 80, 30, "");
Object v2 = graph.insertVertex(vDogsRoot, null, "Shar Pei", 0, 0, 80, 30, "");
Object v3 = graph.insertVertex(vDogsRoot, null, "Pug", 0, 0, 80, 30, "");
Object v4 = graph.insertVertex(vDogsRoot, null, "Cocker Spaniel", 0, 0, 80, 30, "");
Object v5 = graph.insertVertex(vDogsRoot, null, "Pit Bull", 0, 0, 80, 30, "");
Object v6 = graph.insertVertex(vDogsRoot, null, "Chihuahua", 0, 0, 80, 30, "");
graph.insertEdge(parent, null, "", vDogsRoot, v2);
graph.insertEdge(parent, null, "", vDogsRoot, v3);
graph.insertEdge(parent, null, "", vDogsRoot, v4);
graph.insertEdge(parent, null, "", vDogsRoot, v5);
graph.insertEdge(parent, null, "", vDogsRoot, v6);
mxHierarchicalLayout layout = new mxHierarchicalLayout(graph);
layout.setUseBoundingBox(false);
layout.execute(vDogsRoot); //apply the layout to the root group node.
layout.execute(parent);
}
finally
{
graph.getModel().endUpdate();
}
mxGraphComponent graphComponent = new mxGraphComponent(graph);
getContentPane().add(graphComponent);
}
public static void main(String[] args)
{
HelloWorld frame = new HelloWorld();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 320);
frame.setVisible(true);
}
}
Produces: (notice the collapse button)
How do I prevent the vertices from being inside the parent cell of the cell group? I want the tree hierarchy to be maintained but collapsible. Am I on the correct path using Cell Groups? What I am doing wrong?
I suspect I just need to tell the parent of the cell group (vDogsRoot) to allow cells to be draw outside its boundary but I do not see a way to do this yet. Or maybe I am taking a totally wrong approach. Thinking this should be a trivial thing to accomplish yet I have tried many different things and googled/read many docs and no success yet.
UPDATE 1:
Groups are not what I am needing here. I just need to traverse the directed tree and toggle showing the nodes below the selected node. Found a java script example named tree.html in the mxGraph examples folder. Just need to convert that example from JavaScript to Java.