SwingNode “integration” part II – The Myth not the reality


Today, just before the end of the world (12/21/12 GMT +1 :-)), I feel like David Copperfield who would reveal a magic trick… And sometimes, the magic should stay magic… but we’re still talking about programming here, so even if I hope there are still magicians in the UI world I would apologize to all Java UI gurus, architects and evanglists who could be shocked by the current experimental approach ;-).
Ok, disclaimer done.

In the first post, you may have noticed the quote in the title when I wrote “integration” and you were right because the current implementation relies on a visual trick which consists on superposing a Swing Window on top of a JavaFX application…
As a short video:

Here are the layers:

Image
The undecorated Swing window is synchronized with all sizes and locations of the underneath SwingNode JavaFX placeholder.
The synchronization between Swing window’s bounds and with the SwingNode placeholder was not an issue (BTW performance are quite good with a simple UI). The main problem was to ensure the Swing window to be displayed on top of the JavaFX stage. And at this time, I made it working by using the “AlwaysOnTop” property of the Swing window, which could dramatically reduce the scope of the approach.

Here is a summary of the limitations at this time:

  •  Limited to one SwingComponent for a JavaFX app
  •  “Always on top” makes the jDialog on top of Windows’ task bar
  •  When clicking on the Swing component, the JavaFX Stage looses the focus an so the stage window looses its drop shadow.
  •  Not optimal for applet mode. Offset are not good and clipping from browser must be used.
  •  Dealing with JavaFX and Swing threads in the same app is not trivial.

Code

Test it on your machine by clicking on this executable macJar

Access to the code, project and binaries  here:github-logo-transparent

If you want to integrate into your app , simply add SwingNode.jar into your ClassPath and invoke:

Node swingNode = new SwingNode(Stage jfxStage, Component jcomponent);

The experiment is not finished. There are still some graphical artifact such as when the Swing window is stuck on top…
I also tried to implement it using multiple SwingNode into a single JavaFX application but since I currently relies on the “AlwaysOnTop” window setting this techniques is very limited, and dealing with many different windows types is not optimal for apps!

Lesson learnt: This kind of “integration” could be realistic if we could easily declare AWT windows as child of a JavaFX stage…

Mac Support?
WORA…hum not yet ! Window management and threading seems to be slightly different on this platform ’cause my code simply doesn’t work. It throws an exception and the stage never appears… why? because http://javafx-jira.kenai.com/browse/RT-20784 Which is not related to this approach since a basic a Swing/JavaFX mixing hangs on Mac when launching from a JavaFX app (JFXPanel works fine in a Swing app on Mac)

Conclusion
At this stage, use this technique in case of emergency only!

 (or wait for a real good integration!)

C ya!

About these ads
  1. #1 by Kai on 23/12/2012 - 21:45

    Thanks for sharing your code. To make it run I had to uncomment line 178 in SwingNode.

    • #2 by arnaud nouard on 23/12/2012 - 22:24

      Thanks a lot Kai, this was due to a last minute test…
      Fixed.

  2. #3 by patmartin on 10/01/2013 - 19:20

    This is terrific and better than what I was doing previously. Thanks Arnaud!

  3. #4 by mjawath on 05/02/2013 - 07:41

    guys what do u think about integrating swing inside javafx components .isn’t it a must Todo on oracles Javafx path . i wonder why oracle is not giving any priority for that ?

    • #5 by arnaud nouard on 05/02/2013 - 09:29

      AFAIK, Swing inside JavaFX is now an official feature of Java8. Stay tuned!

  4. #6 by Bastien on 21/02/2013 - 21:56

    Hi, nice stuff, can I use it in an application ? no licence or something like that ?

    • #7 by arnaud nouard on 21/02/2013 - 22:44

      Hi Bastien and thanks for your feedback,
      You can use this code for any kind of purpose, commercial or not. I’ll add BSD headers.
      Thanks.

  5. #8 by Muhammad Javed on 14/05/2014 - 16:46

    Hi Arnaud,

    Please discard my previous msg.

    I am running your SwnigNodeDemo code on MAC / Eclipse / Java8.0

    When I run the Demo code, I get no exception but JavaFX GUI do not show up.

    Any insight here ?

    Mainly I am having issue with setting size of JPanel embedded in SwingNode.
    http://stackoverflow.com/questions/23655012/unable-to-change-size-of-jbutton-added-to-jpanel-embedded-into-swingnode-java

    Thanks

    • #9 by arnaud nouard on 14/05/2014 - 22:03

      Hi,
      My old SwingNode experiment is no longer applicable if you are using Java8. This release contains a real and supported SwingNode in the JavaFX package.
      Moreover, my project cannot run on a Mac due to a current limitation when invoking both AWT and JavaFX in an application (https://javafx-jira.kenai.com/browse/RT-20784).
      Hope this helps.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: