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)
or
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!

Solution:

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

Leave a Reply to micropigmentación capilar Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

3 thoughts on “JFXPanel and FX Platform Thread pitfalls”

  1. I encountered this FX thread termination issue as well, thank you for this solution to an issue that is not well documented!

  2. marbella

    Good post. I learn something totally new and challenging
    on sites I stumbleupon everyday. It will always be exciting to read content from
    other writers and practice something from their websites.