首页 > 解决方案 > javaFX Linechart 的工具提示中的错误

问题描述

我正在使用 JavaFX 线图。我正在尝试在折线图上设置工具提示。我在事件句柄中收到错误运行时错误。代码有什么问题?

public class ChartPlot 
{
    static LineChart <Number,Number> linechart;
    static double[] xArray, yArray;
    static ArrayList <Double> xList, yList;
    public static XYChart.Series series;
    public static LineChart linePlot(double[] x, double[] y) {
        xArray = new double[x.length];
        yArray = new double[y.length];

        xArray = x;
        yArray = y;

        //Defining the x axis             
        final NumberAxis xAxis = new NumberAxis();
        xAxis.setLabel("Wavelength");

        //Defining the y axis   
        final NumberAxis yAxis = new NumberAxis();
        yAxis.setLabel("Intensity");

        //Creating the line chart 
        linechart = new LineChart <>(xAxis, yAxis);

        linechart.getData().clear();
        //Prepare XYChart.Series objects by setting data 
        series = new XYChart.Series();

        //Setting the data to Line chart  
        for (int i = 0; i < xArray.length; i++) {
            series.getData().add(new XYChart.Data(xArray[i], yArray[i]));
        }

        linechart.setCreateSymbols(false);

        linechart.getData().add(series);

        xAxis.setAutoRanging(true);
        xAxis.setForceZeroInRange(false);
        yAxis.setAutoRanging(true);
        yAxis.setForceZeroInRange(false);
        linechart.autosize();
        linechart.applyCss();
        String css = FXMLDocumentController.class.getResource("LSG.css").toExternalForm();
        linechart.getStylesheets().add(css);
        linechart.setLegendVisible(false);

        for (XYChart.Series <Number, Number> s: linechart.getData()) {
            for (XYChart.Data <Number, Number> d: s.getData()) {
                Tooltip.install(d.getNode(), new Tooltip(
                d.getXValue().toString() + "," + d.getYValue()));

                //Adding Class on Hover or on click
                d.getNode().addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler < MouseEvent > () //----- This is the line I am getting error
                {@Override
                    public void handle(MouseEvent event) {
                        System.out.println("X :" + d.getXValue() + " Y :" + d.getYValue());
                    }
                });
            }
        }

        return linechart;

    }
}

我正在使用打开按钮来读取文件和绘图。在绘图时,我试图插入侦听器以显示我单击的值。

标签: javafxlinechart

解决方案


推荐阅读