Question

Why at first works chartMouseClicked (JFreeChart library), and already then mouseClicked?

boolean isDoubleClicked = false;
chartPanel.addMouseListener(new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent me) {
            if (me.getClickCount() == 2 &&) {
                isDoubleClicked = true;
            }
        }
        @Override
        public void mousePressed(MouseEvent me) {}
        @Override
        public void mouseReleased(MouseEvent me) {}
        @Override
        public void mouseEntered(MouseEvent me) {}
        @Override
        public void mouseExited(MouseEvent me) {}
    });



chartPanel.addChartMouseListener(new ChartMouseListener() {
        @Override
        public void chartMouseClicked(ChartMouseEvent cme) {
            if (isDoubleClicked)
                System.out.println("Double clicked!");
        }
        @Override
        public void chartMouseMoved(ChartMouseEvent cme) {}
    });

So, System.out.println("Double clicked!"); not works. How to correct it?

Was it helpful?

Solution

You have two different listener objects here, one is a MouseListener instance (that listens to mouse events on the panel) and the other is a ChartMouseListener instance (that listens to mouse events on the chart in the panel). They are registered in separate listener lists, and the isDoubleClicked field from one object isn't visible to the other object.

The reason that ChartMouseListener is separate from MouseListener is that JFreeChart creates its own events that contain additional information about the entity in a chart that is "underneath" the mouse pointer.

OTHER TIPS

To add up on @DavidGilbert, you can also use ChartMouseEvent.getTrigger().getClickCount() to detect double-click in the chart.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top