简体   繁体   English

JGraphX-展开/折叠单元格/顶点

[英]JGraphX - Expand/Collapse of a cell/vertex

I need to have the expand/collapse feature with my mxGraph. 我的mxGraph需要具有展开/折叠功能。

The scenario is like, there are three vertices say v1, v2 and v3. 场景就像,有三个顶点,分别是v1,v2和v3。 v2 and v3 are linked to v1. v2和v3链接到v1。 Now there is small icon in vertex v1 and when user clicks on the icon the vertex v2 and v3 should be hided inside v1 and when user clicks on the icon the vertex v2 and v3 should be visible. 现在,顶点v1中有一个小图标,当用户单击该图标时,顶点v2和v3应该隐藏在v1内,并且当用户单击该图标时,顶点v2和v3应该可见。 It is like expand and collapse of vertex v1. 就像顶点v1的展开和折叠一样。

I tried with graph.foldCells() and graph.groupCells(). 我尝试了graph.foldCells()和graph.groupCells()。 But nothing works. 但是没有任何效果。 below is my code. 下面是我的代码。

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

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

public class MxGraphSample {

    public static void creategraph() {

        final JFrame frame = new JFrame();
        frame.setMaximumSize(new Dimension(800, 1200));
        JPanel panel = new JPanel();
        panel.setSize(frame.getMaximumSize().width,
                frame.getMaximumSize().height);

        final mxGraph graph = new mxGraph();
        Object parent = graph.getDefaultParent();
        graph.getModel().beginUpdate();

        try {
            Object v1 = graph.insertVertex(parent, null, "v1", 20, 20, 80, 30);
            Object v2 = graph.insertVertex(parent, null, "v2", 120, 70, 80, 30);
            Object v3 = graph.insertVertex(parent, null, "v3", 220, 70, 80, 30,
                    "fillColor=lightgreen");

            graph.insertEdge(parent, null, "", v1, v2);
            graph.insertEdge(parent, null, "", v1, v3, "strokeColor=lightgreen");

            graph.foldCells(true);
            graph.cellsFolded(new Object[] { v1, v2, v3 }, true, false);

            mxHierarchicalLayout layout = new mxHierarchicalLayout(graph,
                    SwingConstants.WEST);
            layout.setInterRankCellSpacing(70);
            layout.execute(graph.getDefaultParent());

        } finally {
            graph.getModel().endUpdate();
        }
        final mxGraphComponent graphComponent = new mxGraphComponent(graph);

        panel.setLayout(new BorderLayout());
        panel.add(graphComponent, BorderLayout.CENTER);

        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        creategraph();
    }
}

在此处输入图片说明

Any examples will be helpful. 任何示例都将有所帮助。

make the parent of v2 and v3 as v1 instead of defaultParent() 将v2和v3的父级设为v1而不是defaultParent()

              Object v2 = graph.insertVertex(v1, null, "v2", 120, 70, 80, 30);
        Object v3 = graph.insertVertex(v1, null, "v3", 220, 70, 80, 30,
                "fillColor=lightgreen");

Else create a group by the following method after declaring the 3 vertices graph.createGroupCells(new Object[] {v1}); 否则,在声明3个顶点图之后,通过以下方法创建一个组。 graph.groupCells(v1,30.5,new Object[] {v2,v3}); graph.groupCells(v1,30.5,new Object [] {v2,v3});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM