在顶点上添加一个 mouseListener

Add a mouseListener on a Vertex

我正在尝试在 Java 中使用 JGraphX 制作图表并在特定类型的顶点上添加 mouseListener

我尝试使用 graphComponent 但它什么也没做...

这是我的代码:

        mxGraphComponent graphComponent = new mxGraphComponent(graph);

        graphComponent.getGraphControl().addMouseListener( new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                mxCell cell = (mxCell) graphComponent.getCellAt(e.getX(), e.getY());
                System.out.println("TOTO");

                if(cell != null && cell instanceof mxCell){
                    if(cell.getValue().toString().equals("AAM")){
                        System.out.println("My Cell selected");
                    }

                }
            }
        });

你的代码对我来说工作正常。试试这个,它应该 100% 有效:

public class HelloWorld extends JFrame
{
    private static final long serialVersionUID = -2707712944901661771L;

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

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

        graph.getModel().beginUpdate();
        try
        {
            String s1 = "Hello";
            String s2 = "World!";
            Object v1 = graph.insertVertex(parent, "ID1", s1, 20, 20, 80, 30);
            Object v2 = graph.insertVertex(parent, "ID2", s2, 240, 150, 80, 30);
            Object edge = graph.insertEdge(parent, "ID3", "TEST", v1, v2);
        }
        finally
        {
            graph.getModel().endUpdate();
        }
        final mxGraphComponent graphComponent = new mxGraphComponent(graph);

        graphComponent.getGraphControl().addMouseListener(new MouseAdapter()
        {
            @Override
            public void mousePressed(MouseEvent e)
            {
                Object cell = graphComponent.getCellAt(e.getX(), e.getY());
                if (cell instanceof mxCell)
                {
                    System.out.println(((mxCell) cell).getValue());
                }
            }
        });
        getContentPane().add(graphComponent);
    }

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