Eclipse SWT Editor上的JavaFX FXCanvas抛出java.lang.IllegalStateException

时间:2017-04-12 08:20:47

标签: eclipse javafx eclipse-plugin swt eclipse-rcp

Usecase:使用FxCanvas在Eclipse Editor的createPartControl()中显示JavaFx条形图。

问题:在第一次启动RCP应用程序时,swtFxBarChart()成功地在SWT Composite上绘制图形。但是,当我们关闭编辑器窗口并重新打开fxCanvas.setScene(场景)时; 抛出java.lang.IllegalStateException:不在FX应用程序线程上; currentThread = JavaFX应用程序线程

代码:

private void swtFxBarChart(Composite composite) {
        final FXCanvas fxCanvas = new FXCanvas(composite, SWT.NONE);
        fxCanvas.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).align(SWT.FILL, SWT.FILL).create());
        fxCanvas.setLayout(GridLayoutFactory.fillDefaults().create());

        final CategoryAxis xAxis = new CategoryAxis();
        final NumberAxis yAxis = new NumberAxis();
        final BarChart<String, Number> bc = new BarChart<String, Number>(xAxis, yAxis);
        bc.setTitle("Example");
        xAxis.setLabel("Status");
        yAxis.setLabel("Count");
        bc.setLegendVisible(false);

        XYChart.Series series1 = new XYChart.Series();
        final XYChart.Data completedSeries = new XYChart.Data("Completed", completedList().size());
        series1.getData().add(completedSeries);
        final XYChart.Data failedSeries = new XYChart.Data("Failed", failedList().size());
        series1.getData().add(failedSeries);    
        Scene scene = new Scene(bc, 400, 400);
        bc.getData().addAll(series1);
        fxCanvas.setScene(scene);
    }

环境:jdk1.8.0_102

1 个答案:

答案 0 :(得分:0)

在调试问题之后,我发现JavaFx应用程序会在编辑器第一次关闭时自行处理,因此抛出以下异常

java.lang.IllegalStateException:不在FX应用程序线程上; currentThread = JavaFX应用程序线程 在重新打开编辑器

时解决了IllegalStateException

解决方案: -

在创建FxCanvas

之前添加以下代码行
Platform.setImplicitExit(false);

将ImplicitExit设置为false可确保即使在最后一个窗口关闭后应用程序仍将继续正常运行,直到应用程序调用exit。默认值是true。

这解决了我的问题。