Search code examples
javaclicklistenerjfreechart

JFreeChart XYPlot XYImageAnnotation mouse click listener


I have a JFreeChart with 2 subplots (XYPlot).

I have XYImageAnnotations that I want to become buttons, but I can't find a way to listen mouse click on any annotation.

Do you know any way to do this?


Solution

  • I have no idea how to listen [for] click from annotations

    In your ChartMouseListener, you'll get a ChartMouseEvent. Use it to get the ChartEntity that's a XYAnnotationEntity.

    chartPanel.addChartMouseListener(new ChartMouseListener() {
    
        @Override
        public void chartMouseClicked(ChartMouseEvent cme) {
            ChartEntity ce = cme.getEntity();
            if (ce instanceof XYAnnotationEntity) {
                // handle the click
            }
        }
    }
    

    Otherwise, look for the XYItemEntity to which you added the annotation.