Mixing JavaFX with Swing in different windows

Jeff Martin jeff at reportmill.com
Tue Apr 24 08:24:53 PDT 2012

I have had general success with this - the only real trick I think is to create/modify your JavaFX scene from Platform.runLater(), which is a little bit of a dance. Below is some code that is working for me.

The exception is that that when I remove the JFXPanel, then try to re-install it later, I get an exception along the lines of "Platform.exit() has already been called", which I think happens when JavaFX figures out that there is no more visible JavaFX UI.


On Apr 24, 2012, at 9:04 AM, Josh Marinacci wrote:

> Has anyone had luck building an app with one window that is Swing and the other is JavaFX?  I want to slowly moving my large app (LeonardoSketch.org)  over to JavaFX.  
> I have built a new window in JavaFX but I get some init errors on startup. Instead I can put the FX content inside of a JXPanel, but many things break on the FX side like drag and drop. I would rather run the FX content in a real FX stage.  I am fine having one window be run on the FX thread and one on the Swing thread with me being responsible for synchronization via invokeLater calls.  I'm wondering if there is any magic init-mojo required.
> Thanks
> Josh

 * Returns the JavaFX panel.
public JFXPanel getJavaFXPanel()
    // If panel not set, create and set
    if(_jfxPanel==null) {
        _jfxPanel = new JFXPanel();
        Platform.runLater(new Runnable() { public void run() { initFX(); }});
    // Return panel
    return _jfxPanel;

 * Load JavaFX panel.
private void initFX()
    // This method is invoked on the JavaFX thread
    Scene scene = getScene();
    // Add to Swing panel
    SwingUtilities.invokeLater(new Runnable() { public void run() {
        _contentPane.revalidate(); _contentPane.repaint();

More information about the openjfx-dev mailing list