JavaFX 为折线图创建系列:null

JavaFX creating series for linechart: null

我正在创建一个新的 window 控制器,我在其中处理要放入线图中的数据。

尽管最后新的 window 显示了一个空的折线图。当我调试将系列放入折线图中的行时,IDE 状态为 "series[null]"。

那我错过了什么?

Class 新window:

public class 图表窗口 {

Map<Job, List<LoadTestResultStatus>> lTRSMapOfJobs;

final LineChart lineChartMain;

Job originJob;

XYChart.Series series;

boolean lTRSfilled=false;

ObjectHub objectHub;

public ChartWindow(Job job, ObjectHub objectHub) {
    this.objectHub = objectHub;

    series = new XYChart.Series();

    final NumberAxis xAxis = new NumberAxis();
    final NumberAxis yAxis = new NumberAxis();

    xAxis.setLabel("Time");
    yAxis.setLabel("Time");

    lineChartMain = new LineChart<Number, Number>(xAxis, yAxis);

    lTRSMapOfJobs = new HashMap<>();

    originJob = job;

    lTRSMapOfJobs.put(job, objectHub.getDbManagement().getLTRSListOfJobFromDB(job));

    fillLineChart();

}

public void fillLineChart() {

    Platform.runLater(new Runnable() {

        @Override
        public void run() {
            objectHub.getGuiReporter().statusStart(0);
            objectHub.getGuiReporter().progressbarAddAmountOfStep(lTRSMapOfJobs.get(originJob).size());

        }
    });

    for (LoadTestResultStatus l : lTRSMapOfJobs.get(originJob)) {
        objectHub.getExecutor().submit(new Runnable() {

            @Override
            public void run() {
                XYChart.Data xyChart = new XYChart.Data(l.getTs(), l.getLt());
                try {
                    addToSeries(xyChart);
                    objectHub.getGuiReporter().progressbarAddStep();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
    }

    //TODO heavy CPU load:(
    while(!lTRSfilled){
        if(ObjectHub.getGuiReporter().progressBarDifference()==0){
            lTRSfilled = true;
        }
    }
    lineChartMain.getData().addAll(series);
}


void addToSeries(XYChart.Data xyChart) {
    synchronized (this) {
        series.getData().add(xyChart);
    }
}

public Map<Job, List<LoadTestResultStatus>> getlTRSMapOfJobs() {
    return lTRSMapOfJobs;
}

public void setlTRSMapOfJobs(Map<Job, List<LoadTestResultStatus>> lTRSMapOfJobs) {
    this.lTRSMapOfJobs = lTRSMapOfJobs;
}

public LineChart getLineChartMain() {
    return lineChartMain;
}

}

处理新window的MainController方法:

 public void showLineChartForJob() {
    progressBarLabel.setText("Preparing Linechart calculation...");

   ChartWindow object to request executorservices

    Thread createChartWindow = new Thread(new Runnable() {

        @Override
        public void run() {

            int jobId = Integer.parseInt(visualizeTabIdInputTextField.getText());

            Job lineChartJob = new Job(objectHub);

            for (Job j : objectHub.getLtResultManagement().getjobSet()) {
                if (j.getNumber() == jobId) {
                    lineChartJob = j;
                    break;
                }
            }

            chartWindowList.add(new ChartWindow(lineChartJob, objectHub));

            Platform.runLater(new Runnable() {

                @Override
                public void run() {
                    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/ChartWindow.fxml"));
                    fxmlLoader.setController(chartWindowList.get(0));
                    StackPane secondaryLayout = new StackPane();
                    try {
                        secondaryLayout.getChildren().setAll(Collections.singleton(fxmlLoader.load()));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    Scene secondScene = new Scene(secondaryLayout, 1200, 1600);

                    Stage newWindow = new Stage();
                    newWindow.setTitle("Second Stage");
                    newWindow.setScene(secondScene);


                    newWindow.setX(50);
                    newWindow.setY(50);

                    newWindow.show();
                }
            });
        }
    });

    createChartWindow.setName("createChartWindowThread");
    createChartWindow.start();
}

在第一个控制器的最后一行进行的调试捕获:

此致

好的,结案:

失败的原因是没有把现场的情况写到折线图上:

 Scene secondScene = new Scene(secondaryLayout, 1200, 1600);

通过从控制器获取图表修复:

Scene secondScene = new Scene(chartWindowList.get(0).lineChartMain, 1200, 1600);