简体   繁体   中英

How to add a mouselistener to a vertex - jgraphx

How can I add a mouselistener to a specific vertex in jgraphx?

graphComponent.getGraphControl().addMouseListener(new MouseAdapter() {
                    public void mouseReleased (MouseEvent e1) {

I can use this fro graphcomponent but how can I specify it for a vertex?

You can call the graphcomponent class's getCellAt(int x, int y) method with the MouseEvent class's getX() and getY() methods. This will return you an object if there is a vertex (or edge) where you clicked, then with a simple comparison you can decide which vertex is it.

Here is an example:

graphComponent.getGraphControl().addMouseListener(new MouseAdapter() 
{
@Override
    public void mouseReleased(MouseEvent e) 
    {    
        mxCell cell =(mxCell) getGraphComponent().getCellAt(e.getX(), e.getY());
        if(cell != null && cell.equals(YOUR_VERTEX))
        {
            //specific thing you want to do on click
        }
    }
});

You could create an mxCellHandler for a given cell state , ie your specific vertex. You could check the createHandler() of the mxGraphComponent class.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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