JavaFX 2.1: Toolkit not initialized

Javafx 2

Javafx 2 Problem Overview


My application is Swing-based. I would like to introduce JavaFX and configure it to render a Scene on a secondary display. I could use a JFrame to hold a JFXPanel which could hold a JFXPanel but I would like to achieve this with JavaFX API.

Subclassing com.sun.glass.ui.Application and using Application.launch(this) is not an option because the invoking thread would be blocked.

When instantiating a Stage from Swing EDT, the error I get is:

java.lang.IllegalStateException: Toolkit not initialized

Any pointers?


EDIT: Conclusions

Problem: Non-trivial Swing GUI application needs to run JavaFX components. Application's startup process initializes the GUI after starting up a dependent service layer.

Solutions

Subclass JavaFX Application class and run it in a separate thread e.g.:

public class JavaFXInitializer extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        // JavaFX should be initialized
        someGlobalVar.setInitialized(true);
    }
}

Sidenote: Because Application.launch() method takes a Class<? extends Application> as an argument, one has to use a global variable to signal JavaFX environment has been initialized.

Alternative approach: instantiate JFXPanel in Swing Event Dispatcher Thread:

final CountDownLatch latch = new CountDownLatch(1);
SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        new JFXPanel(); // initializes JavaFX environment
        latch.countDown();
    }
});
latch.await();

By using this approach the calling thread will wait until JavaFX environment is set up.

Pick any solution you see fit. I went with the second one because it doesn't need a global variable to signal the initialization of JavaFX environment and also doesn't waste a thread.

Javafx 2 Solutions


Solution 1 - Javafx 2

Found a solution. If I just create a JFXPanel from Swing EDT before invoking JavaFX Platform.runLater it works. I don't know how reliable this solution is, I might choose JFXPanel and JFrame if turns out to be unstable.

public class BootJavaFX {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JFXPanel(); // this will prepare JavaFX toolkit and environment
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        StageBuilder.create()
                                .scene(SceneBuilder.create()
                                        .width(320)
                                        .height(240)
                                        .root(LabelBuilder.create()
                                                .font(Font.font("Arial", 54))
                                                .text("JavaFX")
                                                .build())
                                        .build())
                                .onCloseRequest(new EventHandler<WindowEvent>() {
                                    @Override
                                    public void handle(WindowEvent windowEvent) {
                                        System.exit(0);
                                    }
                                })
                                .build()
                                .show();
                    }
                });
            }
        });
    }
}

Solution 2 - Javafx 2

The only way to work with JavaFX is to subclass Application or use JFXPanel, exactly because they prepare env and toolkit.

Blocking thread can be solved by using new Thread(...).

Although I suggest to use JFXPanel if you are using JavaFX in the same VM as Swing/AWT, you can find more details here: https://stackoverflow.com/questions/11119227/is-it-ok-to-use-awt-with-javafx

Solution 3 - Javafx 2

Since JavaFX 9, you can run JavaFX application without extending Application class, by calling Platform.startup():

Platform.startup(() ->
{
    // This block will be executed on JavaFX Thread
});

> This method starts the JavaFX runtime.

Solution 4 - Javafx 2

I checked the source code and this is to initialize it

com.sun.javafx.application.PlatformImpl.startup(()->{});

and to exit it

com.sun.javafx.application.PlatformImpl.exit();

Solution 5 - Javafx 2

I used following when creating unittests for testing javaFX tableview updates

public class testingTableView {
        @BeforeClass
        public static void initToolkit() throws InterruptedException
        {
            final CountDownLatch latch = new CountDownLatch(1);
            SwingUtilities.invokeLater(() -> {
                new JFXPanel(); // initializes JavaFX environment
                latch.countDown();
            });
    
            if (!latch.await(5L, TimeUnit.SECONDS))
                throw new ExceptionInInitializerError();
        }
    
        @Test
        public void updateTableView() throws Exception {
    
            TableView<yourclassDefiningEntries> yourTable = new TableView<>();
            .... do your testing stuff
           
        }
    }

even though this post is not test related, then it helped me to get my unittest to work

  • without the BeforeClass initToolkit, then the instantiation of TableView in the unittest would yield a message of missing toolkit

Solution 6 - Javafx 2

There's also way to initialize toolkit explicitly, by calling: com.sun.javafx.application.PlatformImpl#startup(Runnable)

Little bit hacky, due to using *Impl, but is useful, if you don't want to use Application or JXFPanel for some reason.

re-posting myself from this post

Solution 7 - Javafx 2

private static Thread thread;

public static void main(String[] args) {
	
	Main main = new Main();
	startup(main);
	thread = new Thread(main);
	thread.start();
}

public static void startup(Runnable r) {
	com.sun.javafx.application.PlatformImpl.startup(r);
}

@Override
public void run() {
	SoundPlayer.play("BelievexBelieve.mp3");
}

This is my solution. The class is named Main and implements Runnable. Method startup(Runnable r) is the key.

Solution 8 - Javafx 2

Using Jack Lin’s answer, I found that it fired off the run() twice. With a few modifications that also made the answer more concise, I offer the following;

import com.sun.javafx.application.PlatformImpl;

public class MyFxTest implements Runnable {

    public static void main(String[] args) {

	    MyFxTest main = new MyFxTest();
	    PlatformImpl.startup((Runnable) main);
    }

    @Override
    public void run() {

	    // do your testing;
	    System.out.println("Here 'tis");
	    System.exit(0); // Optional
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDan VulpeView Question on Stackoverflow
Solution 1 - Javafx 2Dan VulpeView Answer on Stackoverflow
Solution 2 - Javafx 2Sergey GrinevView Answer on Stackoverflow
Solution 3 - Javafx 2Eng.FouadView Answer on Stackoverflow
Solution 4 - Javafx 2user28775View Answer on Stackoverflow
Solution 5 - Javafx 2serupView Answer on Stackoverflow
Solution 6 - Javafx 2krzychekView Answer on Stackoverflow
Solution 7 - Javafx 2Jack LinView Answer on Stackoverflow
Solution 8 - Javafx 2Ray2UView Answer on Stackoverflow