The first time I open a Shell and create a new FXCanvas setting the Scene it works fine. The second time the code executes (after closing the Shell and opening a new one) it throws the "Not on FX application thread" exception.
As I debug, when the first new FXCanvas returns I am on the FX thread as indicated by javafx.application.Platform.isFxApplicationThread(). The second time the code runs, the new FXCanvas returns and I am not on the FX thread.
If I wrap the whole process in a Platform.runLater() it runs fine the first execution, but the second execution never executes the code in the runLater().
Let me know if that is unclear.
Thanks.
Java Code:
package temp;
import java.net.URL;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import javafx.embed.swt.FXCanvas;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class SwtWrapper {
public SwtWrapper() {
super();
}
public void go() {
Display display = Display.getDefault();
try {
Shell shell = new Shell(display);
FXCanvas canvas = new FXCanvas(shell, SWT.NONE);
URL url = Class.class.getResource("/temp/Fxml.fxml");
FXMLLoader fxmlLoader = new FXMLLoader(url);
Parent rootNode = fxmlLoader.load();
canvas.setScene(new Scene(rootNode));
canvas.pack();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if ( !display.readAndDispatch() )
display.sleep();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
display.dispose();
}
}
public static void main(String[] args) {
new SwtWrapper().go();
new SwtWrapper().go();
System.exit(0);
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<VBox>
<children>
<Text text="The FX Content." />
<ProgressIndicator />
</children>
</VBox>
Maven Dependencies:
<dependency>
<groupId>org.eclipse</groupId>
<artifactId>swt</artifactId>
<version>4.4-cocoa</version>
<classifier>macosx-x86_64</classifier>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>jfxswt</artifactId>
<version>8.0.0-FINAL</version>
<systemPath>${java.home}/lib/jfxswt.jar</systemPath>
<scope>system</scope>
</dependency>