javavertexjgraphx

determine if two vertices are connected jGraphX


Is there a method to determine if two vertices are connected in JgraphX? The method isConnectable() only returns true if the vertex is connected.


Solution

  • You can check it by checking the edges. In this example, being cell1 the first cell you have and cell2 the cell you want to check if its connected with cell1.

    for (int i = 0; i < cell1.getEdgeCount(); i++) {
       mxCell source = ((mxCell) cell1.getEdgeAt(i)).getSource();
       mxCell target = ((mxCell) cell1.getEdgeAt(i)).getTarget();
       if (source == cell2 || target == cell2)
           return true;
       else
           return false;
    }
    

    You need to check both source and target 'cause you can't be sure if cell1 will be the source or the target in that iteration. This way, you iterate every cell that is connected to cell1 and checks if it is equal to a second cell.