3

I'm using JGraphx to draw a graph inside a Swing Panel and all works fine. I try to insert two cells inside a target cell and is working, but layout is changing and all cells are not horizontal.

So my example is: 1) build a graph with some cells:

Horizontal layout

2) press top botton and add two cells inside Hello:

after insertion

As you can see, problem is layout is not horizontal anymore, even if I call again mxHierarchicalLayout.

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);
    }

}
Frodo Baggins
  • 8,290
  • 6
  • 45
  • 55
Vokail
  • 630
  • 8
  • 27
  • 1
    The problem also occurs when you insert the inner cells already in the constructor, so it's a problem with mxHierarchicalLayout, not with updating/refreshing the graph... – Eric Leibenguth Sep 24 '15 at 10:42
  • @EricLeibenguth do you suggest to open an Issue on library ? – Vokail Sep 24 '15 at 12:19
  • Possibly... Perhaps there's a way to control the behaviour of `mxHierarchicalLayout` by extending it, but I don't know the library that well. – Eric Leibenguth Sep 24 '15 at 12:24
  • Seems to be well known problem: https://github.com/jgraph/jgraphx/issues/5 and here http://forum.jgraph.com/questions/5044/mxhierarchicallayout-odd-placement-of-vertices-vertical-offsets still searching for a solution... – Vokail Sep 24 '15 at 12:50
  • A quick workaround for this is to use mxHierarchicalLayout but also put your coordinate inside cell's geometry. Is NOT the right thing to do, but could save your day – Vokail Oct 20 '15 at 07:12

0 Answers0