Turn A Swing App Into JavaFX In Five Lines Of Code

Update 21 February: I notice on DZone that @kirillcool commented somewhat dismissively, “If only it were so simple.” It’s worth emphasizing, then, that this post is about illustrating just how easy it is for people to get started with JavaFX.  The truth is: it really is this easy to get going. Of course, that is not the same thing as having a robust system deployed to production in an architecturally complex environment – but that is hardly the point.  If you’d like a concrete example of how you can take this idea of mixing Swing and JavaFX a little further, please feel free launch this little JavaFX demo (just click the link to launch it via web start) I wrote, and  read about it on JFXStudio.org. I trust it goes without saying that JavaFX is still a new technology – so expect to have to deal with wrinkles;  if you don’t have the mindset of an early adopter, obviously this won’t be for you.

In the minds of many Enterprise Java developers, there’s a substantial barrier to adopting JavaFX. That is, their huge investment in Swing. The thinking is understandable.  After all, if you have a decade or more of elapsed time, and perhaps thousands of man years invested in building Java Swing applications, there’s no way you’ll want to throw that investment away.  Especially not in the current economic environment.   To make matters worse, they think, you can’t embed JavaFX code inside a Swing application. So, they feel they can’t even take an evolutionary approach to JavaFX adoption.  Right?  Well, errr… no.  The truth is – there’s almost a zero barrier to adopting JavaFX.  There’s just one trick you need to know to get started.

The trick to integrating JavaFX and Swing is to make the outer layer of your application a JavaFX app; and then you’re good to go.  Once you’ve done that, you can mix Swing and JavaFX to your heart’s content.

If you think that sounds like a lot of work, you might be in for a surprise.   You only need five lines of JavaFX Script code to turn a Swing app into a JavaFX app.   You need to encapsulate your Swing app as something like an extended JPanel.  In the JavaFX Script example code below, the Swing class that is the main class for your entire Swing app would be called “UIPanel”…

//Instantiate your Java Swing application
var mySwingApp = new UIPanel();

//Make your swing app embeddable in a JavaFX scene graph
var myJavaFXComponent = SwingComponent.wrap(mySwingApp);

//Create the user interface for the JavaFX application
Stage {
    title: "A Swing App In JavaFX" width: 800 height: 600
    scene: Scene { content: myJavaFXComponent }
}

That’s the complete, finished application.    From here, you’re free to extend your existing Swing app’s functionality with as much, or as little, JavaFX as you like.   Could hardly be easier.

Comments

  1. Ran Biron wrote:

    Yes, but why? Why go to the (truly tiny effort) of using JavaFX if you’re still using Swing as your core? What would you gain?

  2. Indoctrinator wrote:

    Sun tries to indoctrinate the whole Java-community to move over to Javafx. But why? Easy: Sun owns and controls JavaFX, there is no JST and there will never be one. JavaFX is a proprietary technology.

  3. simon wrote:

    @Ran I think the major benefit is that it allows people that want to “start small” with JavaFX to do so.

    For example, someone that’s interested in JavaFX might feel daunted if they have to learn about all the features of JavaFX Script before they can write a real app. They could be daunted in terms of having too much new stuff to learn; and/or daunted in not having a lot of time spare to look at the technology.

    So, if that person knows that they can write most of the app using a technology they already know e.g. use Java for most of the GUI, all the business logic, all the networking/database calls etc., then it can be less of a problem just to get started with JavaFX.

    @Indoctrinator

    Even it were true that JavaFX would always remain a proprietary technology (which it may or may not be – your assertion about “never” isn’t based on fact), indoctrination is surely the wrong word to use.

    People make informed choices to adopt proprietary technologies all the time. The way you write seems to imply that you think no-one ever really chooses to use proprietary software technology except if they’re fooled into doing so. That’s not the case. People assess the value proposition in front of them. Based on its merits, they chose to adopt, or not. Slavery was abolished some time ago – you can’t force anyone to adopt a technology, people have lots of choices. No-one is being forced or indoctrinated. Of course, some people make better choices than others.

  4. jramoyo wrote:

    Title should be “Integrating Swing into JavaFX”

  5. nogridbag wrote:

    Neat, although the app doesn’t respond well to resizing. If I enlarge the window, the animation grids to a hault and the entire UI starts flashing white even if the contents do not have to be repainted. Is there a solution for this?

  6. simon wrote:

    Very true. I’ll post an updated version that behaves better with resizing when I get a minute.

  7. JavaFX Dude wrote:

    Could you please send me the full source for this. There still seems to be a bit of magic someplace that I don’t quite understand.

    Thanks

  8. Frank Greco wrote:

    Both types of animation seem to be jerky. Is this due to GC or some other reason?

    JavaFX has to perform like Flash/Flex to be competitive. Smooth animation is a must.

    Running on WinXP btw.

  9. rk tumuluri wrote:

    The main reason I am experimenting with JavaFX is to make my UI “cool”. (effects, animations, video … ). Using JavaFX as a very thin veneer over a solid Java/Swing base is what I am doing. Amy Fowler, one of the senior members of the JavaFX team has a nice “guide” on this. This was also a session at JavaOne, I believe.

    http://weblogs.java.net/blog/2009/06/10/insiders-guide-mixing-swing-and-javafx

    Larry Ellison has publicly stated his support for JavaFX. Frankly Oracle does’nt have anything better than this anyway. Short of conceding the “rich client” space away, they have no other choice but to “invest heavily” in JavaFX and “fight”.

    /rk

  10. Skeptic Man wrote:

    Just to be clear, when you say “you can mix Swing and JavaFX to your heart’s content”, you don’t define what you mean by “mix”.

    I assume you still cannot replace a Swing button with a JavaFX object, and add it to a Swing container, right?

    The progress control is not added as a child to the swing panel where the two buttons are. It’s actually in the javaFX scene, just overlaid over the swing component.

  11. simon wrote:

    Depends what you mean by “still”. If you want to embed JavaFX inside Swing, you can already do it using the following approach:

    http://jfxtras.org/portal/core/-/wiki/JFXtras/JXScene

    However, this won’t work forever. JavaFX is moving away from a reliance on AWT/Swing. With JavaFX 1.3, there’s much less need to consider using Swing than there was, due to the much improved out-of-the-box GUI components.

  12. Skeptic Man wrote:

    Hi Simon,
    Will there ever be an officially supported way of embedding javaFX objects/scenes into swing containers?

  13. Skeptic Man wrote:

    The reason I ask is because we have a large Swing app, that we would like to gradually turn into a JavaFX app. In addition to using JavaFX for special effects (progress status, etc), it seemed logical to want to slowly replace the swing gui controls in our large Swing app with their javaFX counterparts.

  14. simon wrote:

    I don’t know. I suspect if they were going to do this, they would have done it by now. So, the stance they appear to be taking is:

    - Swing is a mature technology. It will continue to be supported. Can continue to build apps using Swing for some years.

    - New innovation and the bulk of resources will be put into JavaFX. They will move away from Swing completely at the earliest opportunity, so they don’t want to encourage the use of Swing with JavaFX.

    Things can change, but I wouldn’t hold my breath for this one…

Post a Comment

Your email is never published nor shared. Required fields are marked *

*

*