0

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):

enter image description here

Using JGraphX, the closest I can achieve is this: enter image description here

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 );
        } );
    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288
Jesse Barnum
  • 6,507
  • 6
  • 40
  • 69
  • You can position the elements explicitly instead of reyling on the automatic layout. – Olivier Jun 30 '23 at 07:22
  • I'd do that if my actual production application was as simple as this example, but it needs to be rendered for a complex diagram whose connections change every time the process executes. – Jesse Barnum Jul 03 '23 at 13:06

0 Answers0