javagraphicsjgraphx

How can you answer a mouse event in jGraphx?


Using jGraphx, I've been able to build a simple diagram that reflects the arquitecture of a program. This graph is going to be read-only, i.e., the user can change the positions of the vertexes, but nothing else (BTW, I have set editable cells and connectable edges to false, but it is still possible to draw connections (?) ).

The question is that I want to be able to display a context menu when the user clicks on of the vertexes. How can I do that? My code as more or less as follows:

mxGraph graph = new mxGraph();
graph.getModel().beginUpdate();

...
Object box = graph.insertVertex( parent, null, obj.getName(), pos, level, 60, 30 );
inheritanceConnections.add( obj );
boxes.put( obj.getPath(), box );
...

// Draw inheritance vertexes
for(ObjectBag obj: inheritanceConnections) {
    graph.insertEdge( parent, null, "", boxes.get( obj.getPath() ), boxes.get( obj.getParentObject().getPath() ) );
}
graph.getModel().endUpdate()

Basically, first all objects are drawn, and later the connections among them (being an object parent of another object) are set.

I'd like to be able to do something like:

Object box = graph.insertVertex( parent, null, obj.getName(), pos, level, 60, 30 );
box.addMouseListener( new MouseListener....

Thank you.


Solution

  • You can do it like so:

    graph.getGraphComponent().getGraphControl().addMouseListener(...)
    

    And then get the cell for the mouse event:

    @Override
    public void mouseReleased(MouseEvent e) {
        Object cell = graph.getGraphComponent().getCellAt(e.getX(), e.getY());
        ...add logic...
    }
    

    You can then check if cell is not null and is a vertex.