Mixing JavaFX with Swing in different windows

Kevin Rushforth kevin.rushforth at oracle.com
Tue Apr 24 09:13:09 PDT 2012

> I saw that go by last week - though now that I think of it, it seems like it shouldn't be an Application instance method - since my app uses JFXPanel, not Application.

Originally I had it as a static method on Platform, but static state 
isn't good design, so Richard suggested putting it on Application (which 
I also like). However, it does bring up the point you mentioned. I 
brought this up informally to a couple folks around here, but I'll 
summarize it for the list.

    While writing tests for RT-15011 (Provide ability to alter the
    implicit "exit on last window closed" behavior), I realized
    something I hadn't though of earlier. Namely, since Swing (or SWT)
    interop apps don't have an instance of Application, they cannot
    simply call the setImplicitExit() method to change the implcit exit
    behavior. After thinking about it for a while it makes sense given
    that there isn't an FX launcher running, but I wanted to at least
    have the discussion of whether it was too limiting. It means that
    without additional API on JFXPanel (or a property) as mentioned in
    RT-19874, an interop app won't be able to suppress the implicit
    shutdown of FX even after I implement RT-15011.

    I could put the implicitExit attribute on Platform as a static
    (which was my original plan), but when Rich and I talked about it we
    both felt that wasn't as clean -- having static state isn't ideal.

-- Kevin

Jeff Martin wrote:
> I saw that go by last week - though now that I think of it, it seems like it shouldn't be an Application instance method - since my app uses JFXPanel, not Application.
> I also just realized that I do have a case of bringing up a javafx Stage after some Swing stuff has already happened, which I managed to fix by simply creating a bogus JFXPanel.
> jeff
> On Apr 24, 2012, at 10:34 AM, Anthony Petrov wrote:
>> I wonder if http://javafx-jira.kenai.com/browse/RT-15011 can help with removing/re-adding a JFXPanel. JIRA is down currently, but here's some details:
>> http://mail.openjdk.java.net/pipermail/openjfx-dev/2012-April/001202.html
>> --
>> best regards,
>> Anthony
>> On 4/24/2012 7:24 PM, Jeff Martin wrote:
>>> 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.
>>> jeff
>>> 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();
>>>    _jfxPanel.setScene(scene);
>>>        // Add to Swing panel
>>>    SwingUtilities.invokeLater(new Runnable() { public void run() {
>>>        _contentPane.add(_jfxPanel);
>>>        _contentPane.revalidate(); _contentPane.repaint();
>>>    }});
>>> }

More information about the openjfx-dev mailing list