MxHierarchicalLayout does not work as expected

I am using JGraphx to draw a graph inside the Swing Panel and everything is working fine. I'm trying to insert two cells inside the target cell and it works, but the layout changes and all the cells are not horizontal.

So my example: 1) Plot a graph with some cells:

Horizontal layout

2) click the top botton and add two cells inside Hello:

after insertion

As you can see, the issue in the layout is no longer horizontal even if I call mxHierarchicalLayout again.

Thanks for answers!

    package com.mxgraph.examples.swing;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;

import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxGraphModel;
import com.mxgraph.model.mxICell;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;

public class HelloWorld extends JFrame {

    private mxGraphComponent graphComponent;

    private int idgenerator = 0;

    public HelloWorld() {
    super("Hello, World!");
    mxGraph graph = new mxGraph();
    Object parent = graph.getDefaultParent();

    graph.getModel().beginUpdate();
    try {
        Object v1 = graph.insertVertex(parent,
            String.valueOf(++idgenerator), "Hello", 20, 20, 80, 30);
        Object v2 = graph.insertVertex(parent,
            String.valueOf(++idgenerator), "World!", 240, 150, 80, 30);
        graph.insertEdge(parent, String.valueOf(++idgenerator), "Edge", v1,
            v2);
        Object v3 = graph.insertVertex(parent,
            String.valueOf(++idgenerator), "Brando!", 240, 150, 80, 30);
        graph.insertEdge(parent, String.valueOf(++idgenerator), "Edge", v2,
            v3);
        Object v4 = graph.insertVertex(parent,
            String.valueOf(++idgenerator), "Jotaro!", 240, 150, 80, 30);
        graph.insertEdge(parent, String.valueOf(++idgenerator), "Edge", v3,
            v4);

    } finally {
        graph.getModel().endUpdate();
    }

    graphComponent = new mxGraphComponent(graph);
    getContentPane().add(graphComponent, BorderLayout.CENTER);

    graphComponent.getGraphControl().addMouseListener(new MouseAdapter() {

        @Override
        public void mouseReleased(MouseEvent e) {
        Object cell = graphComponent.getCellAt(e.getX(), e.getY());

        if (cell != null) {
            System.out.println("cell=" + graph.getLabel(cell));
            JOptionPane.showMessageDialog(graphComponent, "cell="
                + graph.getLabel(cell));
        }
        }
    });

    layoutGraph(graph);

    JButton change = new JButton("change");
    change.setPreferredSize(new Dimension(100, 50));
    change.setAction(new AbstractAction() {

        private static final long serialVersionUID = -1085992528036117542L;

        @Override
        public void actionPerformed(ActionEvent e) {
        mxGraph graph = graphComponent.getGraph();
        mxGraphModel model = (mxGraphModel) graph.getModel();
        mxCell before = (mxCell) model.getCells().get("2");
        graph = insertIn(graph, before);

        layoutGraph(graph);
        }

        public mxGraph insertIn(mxGraph graph, mxCell target) {
        Object parent = graph.getDefaultParent();

        graph.getModel().beginUpdate();
        try {
            mxGraphModel model = (mxGraphModel) graph.getModel();
            mxCell ed = (mxCell) target.getEdgeAt(0);

            mxICell t = ed.getTarget();

            model.remove(ed);

            Object v1 = graph.insertVertex(parent,
                String.valueOf(++idgenerator), "HERE!", 20, 20, 80,
                30);

            Object v2 = graph.insertVertex(parent,
                String.valueOf(++idgenerator), "HERE2", 20, 20, 80,
                30);

            target.insert((mxICell) v1);
            target.insert((mxICell) v2);
            target.setCollapsed(false);

            graph.insertEdge(parent, String.valueOf(++idgenerator), "",
                v1, v2);

            graph.insertEdge(parent, String.valueOf(++idgenerator), "",
                v2, t);

        } finally {
            graph.getModel().endUpdate();
        }
        return graph;
        }
    });

    getContentPane().add(change, BorderLayout.PAGE_START);

    }

    public void layoutGraph(mxGraph graph) {
    mxHierarchicalLayout layout = new mxHierarchicalLayout(graph);
    layout.setOrientation(SwingConstants.WEST);
    layout.execute(graph.getDefaultParent());
    }

    public static void main(String[] args) {
    HelloWorld frame = new HelloWorld();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(800, 400);
    frame.setVisible(true);
    }

}

      

+3


source to share