Home » Java » Desktop Java » JFXPanel and FX Platform Thread pitfalls

About 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.

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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions

 

Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments