Mixing JavaFX with Swing in different windows

Jeff Martin jeff at reportmill.com
Tue Apr 24 08:44:26 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.

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.


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