I'm currently attempting to use JGraphX to create a diagram in a Swing application. My goal is to achieve something similar to this (this should be a directed graph, I just didn't have arrowheads in my mockup tool):
Using JGraphX, the closest I can achieve is this:
The "Bottom" node should be below the grouping, but it renders next to it.
I'm hoping that somebody can either find the problem with my code, or recommend a different diagram / graphing Java library that can achieve the output I'm looking for.
Here is my current drawing code:
import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;
import javax.swing.*;
import java.awt.*;
public class StaticGraphTest extends JFrame {
public StaticGraphTest() {
mxGraph graph = new mxGraph();
Object parent = graph.getDefaultParent();
graph.getModel().beginUpdate();
try {
Object item1 = graph.insertVertex( parent, null, "Top", 0, 0, 80, 30 );
Object grouping = graph.insertVertex( parent, null, "", 0, 0, 80, 100 );
graph.insertEdge( parent, null, "", item1, grouping );
//Add items inside grouping
Object sub1 = graph.insertVertex( grouping, null, "Subitem A", 0, 0, 80, 30 );
graph.insertEdge( grouping, null, "", grouping, sub1 );
Object sub2 = graph.insertVertex( grouping, null, "Subitem B", 0, 0, 80, 30 );
graph.insertEdge( grouping, null, "", grouping, sub2 );
Object item2 = graph.insertVertex( parent, null, "Bottom", 0, 0, 80, 30 );
graph.insertEdge( parent, null, "", grouping, item2 );
mxHierarchicalLayout layout = new mxHierarchicalLayout( graph );
//layout.setResizeParent( true );
layout.setOrientation( SwingConstants.NORTH );
layout.execute( parent );
} finally {
graph.getModel().endUpdate();
}
mxGraphComponent graphComponent = new mxGraphComponent( graph );
//graphComponent.setEnabled( false ); //Make diagram read-only
getContentPane().setLayout( new BorderLayout() );
getContentPane().add( graphComponent, BorderLayout.CENTER );
}
public static void main( String[] args ) {
EventQueue.invokeLater( ()->{
StaticGraphTest frame = new StaticGraphTest();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize( 400, 320 );
frame.setVisible( true );
} );
}
}