javafxjfreechartjfreechart-fx

how to listen to CTL key in combination with ChartMouseListenerFX


I managed to get together a code that allows me to pick a point on a Jfreechart and select the corresponding point on a JavaFX TableView and select one or multiple points. However it's one or the other situation. What I would like is to be able to listen to the CTL key so the user can pick multiple points only when the CLT Key is down in combination with the click. So my question is where and how can I put the key listener.

XYDataset dataset = createXYDataSet(fieldname1,fieldname2);
            ChartViewer plotframe = new ChartViewer(createXYChart(dataset, fieldname1,fieldname2));
            plotframe.addChartMouseListener(new ChartMouseListenerFX(){

                public void chartMouseClicked(ChartMouseEventFX chartmouseevent) {
                int datapoint;
                selectionmodel.setSelectionMode(SelectionMode.MULTIPLE);
                try {
    
            XYItemEntity ce = (XYItemEntity) chartmouseevent.getEntity();
                    datapoint = ce.getItem();
                    datatable.requestFocus();
                    datatable.getSelectionModel().select(datapoint);
                    datatable.getFocusModel().focus(datapoint);
                    datatable.scrollTo(datapoint);
                }catch (Exception e){   
                }
                }
                public void chartMouseMoved(ChartMouseEventFX chartmouseevent) {
                }   
                    
                }
                        
            });

Solution

  • Just looking at the docs http://www.jfree.org/jfreechart/api/javadoc_old/org/jfree/chart/fx/interaction/ChartMouseEventFX.html#getTrigger-- I see getTrigger will get you the MouseEvent https://docs.oracle.com/javase/8/javafx/api/javafx/scene/input/MouseEvent.html#isControlDown-- which has isControlDown()

    So try boolean isCtrlDown = chartmouseevent.getTrigger().isControlDown();