1

In order to understand better how layout algorithms works in Jgraphx, I've developed a little example to display my issue.

1) In this first example I create a node with two nodes connected, then apply layout algorithm, all works as expected, because I'm using as parent of all nodes and edges default parent

defaultparent

```

package it.test.graphDisplay;

import javax.swing.JFrame;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;

public class GraphDisplayTest {

    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
        JFrame frame = new JFrame();
        frame.setBounds(0, 0, 400, 300);

        mxGraph graph = buildGraph();
        mxGraphComponent graphComponent = new mxGraphComponent(graph);

        frame.getContentPane().add(graphComponent);
        frame.setVisible(true);
        }

        private mxGraph buildGraph() {
        mxGraph graph = new mxGraph();
        Object parent = graph.getDefaultParent();

        graph.getModel().beginUpdate();
        try {
            Object v1 = graph.insertVertex(parent, "V1", "V1", 20, 20,
                80, 30);
            Object v2 = graph.insertVertex(parent, "V2", "V2", 240,
                150, 80, 30);
            graph.insertEdge(parent, "E1", "E1", v1, v2);
            Object v3 = graph.insertVertex(parent, "V3", "V3", 240,
                200, 80, 30);
            graph.insertEdge(parent, "E2", "E2", v1, v3);

            // apply layout to graph
            mxHierarchicalLayout layout = new mxHierarchicalLayout(
                graph);
            layout.setOrientation(SwingConstants.WEST);
            layout.execute(parent);

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

    }

}

2) In this second example I'm using as parent of second and third node first one and then apply layout. Result is the following

package it.test.graphDisplay;

import javax.swing.JFrame;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;

public class GraphDisplayTest {

    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
        JFrame frame = new JFrame();
        frame.setBounds(0, 0, 400, 300);

        mxGraph graph = buildGraph();
        mxGraphComponent graphComponent = new mxGraphComponent(graph);

        frame.getContentPane().add(graphComponent);
        frame.setVisible(true);
        }

        private mxGraph buildGraph() {
        mxGraph graph = new mxGraph();
        Object parent = graph.getDefaultParent();

        graph.getModel().beginUpdate();
        try {
            Object v1 = graph.insertVertex(parent, "V1", "V1", 20, 20,
                80, 30);
            Object v2 = graph.insertVertex(v1, "V2", "V2", 240, 150,
                80, 30);
            graph.insertEdge(v1, "E1", "E1", v1, v2);
            Object v3 = graph.insertVertex(v1, "V3", "V3", 240, 200,
                80, 30);
            graph.insertEdge(parent, "E2", "E2", v1, v3);

            // apply layout to graph
            mxHierarchicalLayout layout = new mxHierarchicalLayout(
                graph);
            layout.setOrientation(SwingConstants.WEST);
            layout.execute(parent);

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

    }

}

second

There is a reason of this behaviour ? I'm not able to find any explanation in documentation.

Thank you!

p.s: opened also an issue here: https://github.com/jgraph/jgraphx/issues/50 no clues so far

Vokail
  • 630
  • 8
  • 27

0 Answers0