0
votes

Usecase : Using FxCanvas to display JavaFx Bar Chart inside createPartControl() of Eclipse Editor.

Problem : On first time launch of RCP application, the swtFxBarChart() plots the graph on SWT Composite successfully. However, when we close the editor window and reopen the fxCanvas.setScene(scene); throws java.lang.IllegalStateException: Not on FX application thread; currentThread = JavaFX Application Thread

Code :

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);
    }

Environment : jdk1.8.0_102

1

1 Answers

0
votes

After debugging the problem, I found out that JavaFx app will dispose itself the first time the editor closes and hence throws the below exception

java.lang.IllegalStateException: Not on FX application thread; currentThread = JavaFX Application Thread Solved the IllegalStateException on reopen of Editor

Solution :-

Add the below line of code before creating the FxCanvas

Platform.setImplicitExit(false);

Setting ImplicitExit to false makes sure the application will continue to run normally even after the last window is closed, until the application calls exit. The default value is true.

This solved my problem.