Desktop Java

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 rrunnable)

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()).

Reference: JFXPanel and FX Platform Thread pitfalls from our JCG partner Jens Deters at the JavaFX Delight blog.

Jens Deters

Jens Deters is a Senior Software Developer working in the domain of Aviation Authorities. His main objectives are RIA and Desktop Applications also he loves to play with IoT related stuff.
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button