JFXPanel and FX Platform Thread pitfalls

The JFXPanel is a component to embed JavaFX content into (legacy ;-)) Swing applications.
Basically it makes it very easy to combine both tookits, but there are some pitfalls to master:
Both UI Toolkits are single threaded (Swing: EDT + JavaFX: FX Platform Thread). When used together you have to deal with these two threads, e.g.
javafx.embed.swing.SwingFXUtils.runOnFxThread(Runnable runnable)
javafx.embed.swing.SwingFXUtils.runOnEDT(Runnable runnable)

The FX Platform Thread is implicitly started in the constructor of the JFXPanel by initFx():

// Initialize FX runtime when the JFXPanel instance is constructed
private synchronized static void initFx() {
    // Note that calling PlatformImpl.startup more than once is OK
    PlatformImpl.startup(new Runnable() {
        @Override public void run() {
            // No need to do anything here

But (if I got it right) JFXPanel overrides addNotify() from Component where a finishListener is added to the FX-Platform ( PlatformImpl.addListener(finishListener)). Platform.exit is then called when the last JFXPanel “dies”.

This might lead into a weird situation:

When JFXPanel is used e.g. with a JDialog: The first call opens the Dialog with a new JFXPanel and all is going well.
But when this Dialog is closed the FX Platform Thread is exited and for some reasons it looks like the second call to open a new Dialog doesn’t start the FX Platform Thread again. So nothing happens on the JFXPanel!


For me it worked to call (somewhere early in main())
to prevent closing the FX Thread implicitly (its closed then by the System.exit()).

One thought on “JFXPanel and FX Platform Thread pitfalls”

Leave a Reply

Your email address will not be published. Required fields are marked *