The latest expert opinions, articles, and guides for the Java professional.

Why I’m so excited about migrating from Swing to JavaFX 2

Yep, another article on JavaFX

Ok, I know what you’re thinking: “Oh, another JavaFX technical article”. You’re right. This is an article about how JavaFX is the best thing since bacon-wrapped Ferraris.


I’ve been thinking recently about what JavaFX brought to me as developer, and earlier this year I gave a presentation at MarsJUG in Marseille and FinistJUG in Brest on the topic (see the slides above). See, I come from the Java EE world, meaning I do a lot of EJBs, JPA and so on. But also a lot of JSF. Years ago, I also did Swing development, and even managed to enjoy it most of the time. So I have to deal with core/business classes but also with UI and UX.

I got seduced by JavaFX 2 at Devoxx France 2012 I went to a presentation by guys from Oracle. I was very disappointed by JavaFX 1 but I decided to give it a try.

I decided to develop an app to test JavaFX 2 and I discovered that it covers a lot of concepts from JEE: the binding, the controllers. But also concepts brought by Qt years ago: building a UI with an XML file. For me it is the really first time we can do real MVC for Java desktop apps. It was very freeing for a guy like me, who is normally pretty strict with architectures. When you’re playing with all of this, it is very convenient and natural (even if you have to forget your old Swing tricks).

Why should you bother with JavaFX?

You may ask “Why trying something else to create UI, and why JavaFX?”. That’s a great question. First of all, I’m always looking for new technologies to be up to date in terms of knowledge. I also strongly believe in desktop apps. Nowadays, everyone is talking about web technologies (Angular.js, jQuery, etc) probably because everyone is disappointed with desktop app development; but I don’t think desktop apps are dead. Historically in Java it was difficult to quickly create a sexy UI in Swing, which is a problem by itself. Users want apps quickly, and it is here where my passion for JavaFX started.

The way of developing software changes. Developers change. Languages change. Practices change. So it goes for Java. For more than a decade, we used Swing in order to create our UIs, with all the difficulties it implies. The way of creating UIs had to change in order to be easier and more flexible, which is exactly where JavaFX brings some new fresh air to the world of Java desktop development.

What sucked about Swing that JavaFX improves

Using Java classes for modeling and creating a UI is not really MVC-oriented. In Swing, you often create a class that extends JFrame in order to create your view. And then you add all possible controls your UI will need.

What you may also do is use your IDE (with some plugins) to create your UI by dragging and dropping your components from a palette to a build area. And then the code is generated for you. The big disadvantage is that this code may not be difficult to understand or not adapted to your practices, so to maintain it a lot of junk code is potentially generated.

In JavaFX 2 things change. Now you have FXML files that are used to create the UI. But what is a FXML file? It is a kind of XML that will contain all the controls of your UI.

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>

<AnchorPane id="AnchorPane" prefHeight="101.0" prefWidth="586.0" xmlns:fx="">
    <Label layoutX="14.0" layoutY="14.0" text="File" />
    <TextField layoutX="44.0" layoutY="11.0" prefWidth="200.0" />
    <Button layoutX="252.0" layoutY="12.0" text="..." />
    <GridPane layoutX="373.0" layoutY="11.0">
        <Label text="Red:" GridPane.columnIndex="0" GridPane.rowIndex="0" />
        <Label text="Green:" GridPane.columnIndex="0" GridPane.rowIndex="1" />
        <Label text="Blue:" GridPane.columnIndex="0" GridPane.rowIndex="2" />
        <Slider max="1.0" GridPane.columnIndex="1" GridPane.rowIndex="0" />
        <Slider max="1.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
        <Slider max="1.0" GridPane.columnIndex="1" GridPane.rowIndex="2" />
    <Button layoutX="291.0" layoutY="12.0" mnemonicParsing="false" onAction="loadFile" text="Load" />

In that example you can notice some things:

  • there are some imports like in a Java class
  • there is no Java code
  • it is pretty simple to understand

This is good news. Indeed, this means you are able to create a view without typing a single line of Java code and it is totally MVC-oriented.

With FXML you can also customize your controls by using CSS:

And if you would like to use stylesheets? It is also supported:

<AnchorPane …>
		<URL value="@/com/rebellabs/jfx/css/Default.css" />

	<Label text="Hello RebelLabs" styleClass="title" />

Great, isn’t it?

Properties and binding

How many times would you like to have been notified when a element was added in a list? Or when the value of a String changes? The Observer/Observable pattern is a solution, but it has to be triggered manually in each situation you would like to update it.

In JavaFX 2, things can be done automatically because a new type of object is available: Properties. A property encapsulates a Java type: String, int, boolean etc, and can have listeners.

For example, there is a StringProperty class for working on Strings, IntegerProperty, BooleanProperty and many more. But you also have the generic ObjectProperty, so that you can encapsulate your own Java object. Let’s take a look how to add a listener to a property:

ObjectProperty<RebelFolk> rebelGeek = new SimpleObjectProperty<>();
rebelGeek.addListener(new ChangeListener<RebelGeek>() {
            public void changed(ObservableValue<? extends RebelGeek> observableValue, RebelGeek oldValue, RebelGeek newValue) {
                // Do awesome things

Nothing more, nothing less. Each time the value of the property changes, the listener is triggered. What can also be done is bind properties together:


Each time the anotherRebelGeek value changes, rebelGeek will be updated with the new value. Look at this another binding example:

IntegerProperty num1 = new SimpleIntegerProperty(10);
IntegerProperty num2 = new SimpleIntegerProperty(20);
IntegerProperty num3 = new SimpleIntegerProperty(30);
IntegerProperty num4 = new SimpleIntegerProperty(40);

NumberBinding operation = Bindings.add(num1.multiply(num4), num2.divide(num3));
System.out.println("How much? " + operation.getValue());

System.out.println("And now? " + operation.getValue());

This will first display 400.67 then 4000.67. Magical isn’t it? So you easily imagine some use cases: resizing a window will resize its elements, typing in a text field will display the conversion from $ to €, etc.

Other cool stuff

There is more than what we’ve already mentioned that are interesting in JavaFX:

  • A chart API is available so that you can create, customize and interact with charts. They are dynamically updated when the data set changes.
  • A media engine allowing you to play songs and video. It is a long way from JMF; streaming is also supported.
  • A web engine providing a full browser based on WebKit will allow you to display web content directly in your app.
  • ImageOps allows you to directly work with image to manipulate their properties and pixels.
  • Creating custom components is easy and allows you to either extend existing controls or create brand new ones in order to fulfill your needs.
  • And in JavaFX 8 (coming with Java 8) you will also have the 3D support–but this is for later ;)

Rapid Application Development with JavaFX

I think a great evolution in Java app development is the possibility to easily customize the UI you’re creating. To be honest, Swing was horrible at natively customizing the components you were using. You had libraries to modify the look and feel but what about corporate UI (in which you have to integrate your company fonts, design and so on)?

In JavaFX, everything is customizable using JavaFX CSS: it is not CSS like you know it in web development, but it’s very similar. You don’t have to write any Java code (in most cases) to change the look and feel of your app and it can be done really quickly. This is really progressive, and if you want to go further you can create your own components from scratch or extending existing ones. For example, if you would like to create a search field with a text field and a search button just next to it you can create a custom component adding a text field and a button and use CSS to customize them.

Ultimately, JavaFX provides you with more tools to do RAD (Rapid Application Development):

  • SceneBuilder allows you to create your UI by drag and dropping components on a scene and then generate the FXML file. You have a CSS previewer built in to see what your CSS looks like.
  • ScenicView allows you to manipulate the components while your app is running. It is very useful to modify the CSS and see the result on the fly before applying it in your code.


JavaFX is the successor of Swing, which will be deprecated, and even though it is still young it will help you a lot. It changes the way we do UI in Java by bringing new concepts, plus it’s easy to learn and use and even fun to try out. As Java developer for many years, JavaFX is a huge step forward for developing desktop apps in Java. But don’t take my word for it–test it yourself!

I’m happy to see your comments below, or tweet your thoughts at @twasyl or @ZeroTurnaround.

Responses (11)

  1. Avatar  

    Vasilios Souvatzis

    November 26, 2013 @ 5:09 pm

    I really like JavaFX. I’m an amateur programmer and my couple of apps are just for fun, but Java’s the only language I know and JavaFX was a logical step.

    Wouldn’t mind messing with .NET for desktop apps but I love Netbeans to death to abandon it for Visual Studio :P

    Documentation and examples aren’t as mature as Swing’s but hey, lots of people are getting involved and producing content. I’m excited to say the least :)

  2. Avatar  

    Thierry Wasylczenko

    November 27, 2013 @ 11:23 am

    Regarding the documentation, have you checked this out?
    Hope it helps :)

  3. Avatar  


    December 13, 2013 @ 9:48 am

    Nice article, but your conclusion is a bit premature. Swing won’t be deprecated in the near future as most development tooling is still written in Swing. I’m not expecting a JavaFX version of IntelliJ in the next 10 years.
    Additionally, I don’t see the FXML format as a real advantage. Everybody’s ranting on the verbosity of Maven’s XML format or Spring’s XML contexts, but when JavaFX uses XML to describe UI’s it’s not that big a deal apparently…
    JavaFX certainly has a great future ahead, but until I see some real world big business applications implemented using it (there will always be richt client applications, not everything is suited for the browser), it remains in my hobby project toolbox.

  4. Avatar  


    December 25, 2013 @ 6:48 am

    Swing *has already been* deprecated.
    Point n. 6 from the JavaFX FAQs:

    6. Is JavaFX replacing Swing as the new client UI library for Java SE?
    Yes. However, Swing will remain part of the Java SE specification for
    the foreseeable future, and therefore included in the JRE. While we
    recommend developers to leverage JavaFX APIs as much as possible when
    building new applications, it is possible to extend a Swing application
    with JavaFX, allowing for a smoother transition.

    Also note that *deprecated* doesn’t mean *removed*.

  5. Avatar  

    SoftMAS Desarrollo de Software

    December 30, 2013 @ 1:50 am

    Excellent, very good article, so much useful. Greetings from SoftMAS team.

  6. Avatar  


    January 2, 2014 @ 10:13 pm

    Like I said, I have yet to see a real large business application written in JavaFX. When applications the size of let’s say NetBeans or IntelliJ get (re)written in JavaFX, then it can be considered as a viable alternative to Swing. Until then the JavaFX dev’s can write anything they want in their FAQs. Saying that Swing is deprecated in favor of JavaFX is giving wrong information and can be classified as a plain marketing trick IMHO.

    JavaFX is still too immature in my eyes to really compete with technologies like Swing or WPF on .NET. Perhaps in a couple of years, but Swing deprecated in favor of JavaFX? In their dreams.

  7. Avatar  


    January 2, 2014 @ 10:16 pm

    +1 to this argument. However, some of the components in NetBeans are already implemented in JavaFx and also IntelliJ devs have been expressing their interest in JavaFx being shipped with JDK by default. So the dream might come true.

    I quite sure NetBeans could be written in JavaFx, but that’s an enormous amount of work, imo it won’t happen just because Swing is deprecated.

  8. Avatar  

    David Ken

    February 11, 2014 @ 9:26 pm

    “The big disadvantage is that this code may not be difficult to understand or not adapted to your practices” Should that first “not” be removed?

  9. Avatar  

    Ken Fogel

    April 3, 2014 @ 6:58 pm

    This was my last year using Swing in my courses. Starting in the Fall my Java Application Project course will use JavaFX as will my evening training courses. I would appreciate any recommendations for material I might use in the classroom.

  10. Avatar  


    August 28, 2014 @ 10:44 pm

    As an early beta Swing developer, I’m especially pleased to see the JavaFX 8 release and community mature well beyond the Swing space. I will never do Swing work again. JavaFX is the future of Java UI work.

    In 2006, I used Spring DI in Swing apps. It was all XML in those days. Later, I was able to use Spring annotations; in particular, the lifecycle annotations to build complex financial Swing applications. Spring and Swing were a marriage made in heaven.

    While you can inject Spring into the JavaFX lifecycle, I have yet to explore the practical usefulness and practicality of Spring DI annotations within a JavaFX app.

    Currently, I’m working with ScalaFX. It’s still early days for me. But I have high hopes in removing Java boilerplate code. And, in lieu of Spring DI, perhaps, traits and package objects might just work beautifully.

    Lastly, I’d like to pass on a bit of advice to all those rich client naysayers. The rich client is far from dead. Swing actually rules the financial space, not to mention other niche spaces. And I suspect JavaFX will as well. The web just doesn’t cut it for anything less than time-insensitive reports.

    But here’s the killer meme — consumers absolutely hate web apps for anything but news, research and shopping. Not unlike engineers, if they must use an app several hours a day, they overwhelmingly prefer using native ( or Java ) apps. We geeks are no different, are we?

    Grok that!;)

  11. Avatar  

    Ruslan López

    January 24, 2016 @ 5:21 pm

    And the components on Netbeans stink… Fields in wich you can’t write until reopen, interfaces not loading at all, glitches in the UI, problems rescaling and ordering, Even if the browser loks great, still JEditorPane seems to be more interaction responsive.

RSS feed for comments on this post.

Leave a comment