用例:使用FxCanvas在Eclipse Editor的createPartControl()內顯示JavaFx Bar Chart。Eclipse SWT編輯器上的JavaFX FXCanvas引發java.lang.IllegalStateException
問題:首次啓動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