Is there any way to delete an edge which is passing from vertex1 - vertex2. Say i have both source and target vertex. Now i need to delete this edge... Because i am connecting that vertex to new one on drag and drop so i must remove its old connection with its parent as the parent has been changed after drag and drop....

Thanks in advance

asked 24 Aug '12, 03:47

hansie's gravatar image

accept rate: 0%

Here's the code:

package com.mxgraph.examples.swing;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

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

public class DeleteExample extends JFrame {

    private static final long serialVersionUID = 8083868183987456695L;

    mxICell a,b,c,d,e,f,g,h;

    public DeleteExample() {

        super("Delete Example");

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

        try {

            a = (mxICell) graph.insertVertex(parent, null, "a", 0, 0, 80, 30);
            b = (mxICell) graph.insertVertex(parent, null, "b", 0, 0, 80, 30);
            c = (mxICell) graph.insertVertex(parent, null, "c", 0, 0, 80, 30);
            d = (mxICell) graph.insertVertex(parent, null, "d", 0, 0, 80, 30);
            e = (mxICell) graph.insertVertex(parent, null, "e", 0, 0, 80, 30);
            f = (mxICell) graph.insertVertex(parent, null, "f", 0, 0, 80, 30);
            g = (mxICell) graph.insertVertex(parent, null, "g", 0, 0, 80, 30);
            h = (mxICell) graph.insertVertex(parent, null, "h", 0, 0, 80, 30);

            graph.insertEdge(parent, null, "", a, b);
            graph.insertEdge(parent, null, "", a, c);
            graph.insertEdge(parent, null, "", b, d);
            graph.insertEdge(parent, null, "", b, e);
            graph.insertEdge(parent, null, "", c, f);
            graph.insertEdge(parent, null, "", c, g);
            graph.insertEdge(parent, null, "", e, h);

        } finally {

        // define layout
        mxIGraphLayout layout = new mxHierarchicalLayout(graph);

        JButton button = new JButton( "Delete edge from 'b' to 'e'");
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                try {

                    Object[] edges = graph.getEdgesBetween( b, e);

                    for( Object edge: edges) {
                        graph.getModel().remove( edge);

                } finally {

        JPanel toolBar = new JPanel();
        toolBar.add( button);

        mxGraphComponent graphComponent = new mxGraphComponent(graph);
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(graphComponent, BorderLayout.CENTER);
        getContentPane().add( toolBar, BorderLayout.NORTH);

    public static void main(String[] args) {
        DeleteExample frame = new DeleteExample();
        frame.setSize(800, 600);

This is what it looks like when you start the frame:

alt text

And this is what it looks like when you hit the "Delete edge from 'b' to 'e'" button:

alt text

Please don't forget to flag your post as "answered". It's kinda annoying to waste time by trying to help and looking into posts that already have the answers in them ;-)


answered 27 Aug '12, 12:00

Lofi's gravatar image

accept rate: 37%

Thanks a lot Lofi, Answered.... :)

(28 Aug '12, 02:24) hansie

This is how you delete an edge programmatically:

e. g. you got the edge of type mxICell:

try {
    graph.getModel().remove( edge);
} finally {

About getting the edge take e. g. a look at's getEdges methods.


answered 25 Aug '12, 11:32

Lofi's gravatar image

accept rate: 37%

edited 25 Aug '12, 11:32

Thank you so much for replying Lofi,

For edge detection : I am doing like this var edge = graph.getEdgesBetween(v1,v2); But i am unable to remove :( , Could u please help me regarding this. Is this is the right command to detect the edge as per your suggestion...

Advance Thanks

(27 Aug '12, 11:41) hansie

Add this to your code:

new mxKeyboardHandler( graphComponent);

It installs a keyboard handler with the mxGraph actions. When you select an edge, you can press "delete" on your keyboard and the edge will be removed.


answered 25 Aug '12, 07:29

Lofi's gravatar image

accept rate: 37%

Thanks for the reply Lofi,

Actually i need to do it programatically something like deleEdge(source,target); Is it possible to do something like this.... Because i have to do it dynamically, i may not know which node to delete so...

Thanks in advance...

(25 Aug '12, 10:21) hansie
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported



Asked: 24 Aug '12, 03:47

Seen: 17,918 times

Last updated: 28 Aug '12, 02:24