简体   繁体   English

如何在JGraphX lib中删除Vertex?

[英]How to remove Vertex in JGraphX lib?

I'm trying to solve a task where i need to display graph in Swing. 我正在尝试解决一个需要在Swing中显示图形的任务。 Found JGraph framework (which is now called JGraphX). 找到了JGraph框架(现在称为JGraphX)。 In samples the graph.insertVertex(...) method is used for adding Vertices and the graph.insertEdge(...) method for Edges - ok it works fine. 在示例中, graph.insertVertex(...)方法用于添加顶点,而graph.insertEdge(...)方法用于graph.insertEdge(...)好的,它工作正常。

But how can i remove that Vertex afterwards? 但是之后我该如何删除该顶点?

It looks like you can use the mxGraph.removeCells method for removing a vertex. 看起来您可以使用mxGraph.removeCells方法删除顶点。 I have modified the HelloWorld example that is included in JGraphX (using release 3.4.1 ): 我已经修改了JGraphX中包含的HelloWorld示例(使用3.4.1版 ):

import com.mxgraph.swing.*;
import com.mxgraph.view.*;
import javax.swing.*;

/**
 * Adapted from https://github.com/jgraph/jgraphx/blob/master/examples
 *              /com/mxgraph/examples/swing/HelloWorld.java
 */
public class GoodbyeVertex extends JFrame {
    private static final long serialVersionUID = -2707712944901661771L;

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

    public GoodbyeVertex() {
        super("Hello, World!");

        mxGraph graph = new mxGraph();
        Object parent = graph.getDefaultParent();

        graph.getModel().beginUpdate();
        try {
            Object v1 = graph.insertVertex(parent, null, "Hello", 20, 20, 80, 30);
            Object v2 = graph.insertVertex(parent, null, "World!", 240, 150, 80, 30);

            graph.insertEdge(parent, null, "Edge", v1, v2);

            // Remove a vertex. The related edge is removed as well.
            graph.removeCells(new Object[]{v1});
        } finally {
            graph.getModel().endUpdate();
        }

        mxGraphComponent graphComponent = new mxGraphComponent(graph);
        getContentPane().add(graphComponent);
    }
}

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

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