I am attempting to utilize the NetBeans Rich Client Platform (RCP) as a shell to manage plugins for an automated trading application which uses JavaFX for the UI, but no Swing components. As such, I have a module installer class which creates the JavaFX scene, but no TopComponent class which RCP developers are accustom to using. The module class I have created is below.
import org.openide.modules.ModuleInstall;
public class Installer extends ModuleInstall {
@Override
public void restored() {
Platform.setImplicitExit(false);
// create JavaFX scene
Platform.runLater(() -> {
Parent root;
try {
FXMLLoader fxmlLoader = new FXMLLoader(MainScreen.class.getResource("/com/zoicapital/qtrader/plugin/BasePanel.fxml"));
root = (Parent) fxmlLoader.load();
Scene scene = new Scene(root);
} catch (Exception ex) {
ex.printStackTrace();
}
});
}
The problem is that when launching the application the following exception is being thrown.
java.lang.IllegalStateException: Toolkit not initialized
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:273)
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:268)
at javafx.application.Platform.runLater(Platform.java:83)
at com.zoicapital.qtrader.plugin.Installer.restored(Installer.java:22)
It turns out that even though Platform.runLater() is being invoked, it does not initialize the JavaFX runtime if it hasn’t already been started.
There are a couple of ways around the issue. The first is to invoke the Application.launch() method, or simply instantiate a new JFXPanel() class (even if it isn’t used for anything). Either action will cause the JavaFX Runtime to start and avoid initialization exception when the application is started.
Hi Rob,
instead of creating an empty JFXPanel() you can also call:
PlatformImpl.startup(() -> {});
which is actually called in JFXPanel’s constructor (via initFX() method) to init/start the FX Application Thread.
Cheers,
Jens
(Maybe also have a look at http://www.jensd.de/wordpress/?p=1843)
Thanks for the tip Jens, I’ll update my post.
Pingback: JavaFX Toolkit Not Initialized (Solved) | Dinesh Ram Kali.
Thank you for your guide! Very useful!
You saved me from a lot of work.
I love you.