mxHierarchicalLayout无法按预期工作

时间:2015-09-24 09:34:36

标签: java swing jgraphx

我使用JGraphx在Swing面板中绘制图表,一切正常。 我尝试在目标单元格中​​插入两个单元格并且正在工作,但布局正在改变,所有单元格都不是水平的。

所以我的例子是: 1)用一些单元格构建图表:

Horizontal layout

2)按顶部按钮并在Hello中添加两个单元格:

after insertion

如您所见,问题是布局不再是水平的,即使我再次调用mxHierarchicalLayout。

感谢您的回答!

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

}

0 个答案:

没有答案