Blog

JavaFX 2.0 beta

31 May, 2011
Xebia Background Header Wave

Last weekend the public beta of JavaFX 2.0 came out. I’ve much anticipated this release, as you might guess from my previous posting on JavaFX 2.0. I’ve downloaded the JavaFX-runtime, SDK and Netbeans-plugins the following evening from Oracle’s JavaFX page and started trying out JavaFX by viewing and running the examples from the SDK from Netbeans.
I’m quite enthusiastic, read on the learn more!
JavaFX-overview
The new JavaFX is a library, as written in my previous posting. The SDK now just consists of documentation (including examples), a jar file and the JavaFX-plugin. You’ll have to install the plugin on your computer before you can use JavaFX, but eventually (probably around Java 7 or 8 ) JavaFX will be included in the default Java-runtime.
Currently, only the Windows versions of the plugin and sdk are ready and available. The Windows version uses DirectX, while Mac and Linux versions use OpenGL as shown in the figure below (from taken from JavaFX Architecture and Framework):
Figure 1 JavaFX Architecture Diagram
Description of Figure 1 follows
Description of “Figure 1 JavaFX Architecture Diagram”
From what I heard previously at the NLJug event, since Windows is still most widely used, that version is released first, versions for other platforms, like MacOS, Linux and other will folllow.
I started trying out JavaFX by using a few examples in Netbeans. I normally use Eclipse for development, but since all examples are Netbeans-projects and I am quite lazy, I decided to another IDE. Note: because JavaFX is library, you’re free to use another IDE (or the command-line) to use JavaFX. Compiling or running JavaFX application is no different then running any other Java-application.
The SDK include a lot of readable examples which demonstrate the capabilities of JavaFX and explain the various techniques you can use.

Here are some highlights:

  • You’ll have to worry less about threads as you had to when using Swing: The event-handling thread is separate from the UI-handling thread. There are three Threads in JavaFX doing their work: Event Dispatch Thread – for handling events, the Renderer thread for rendering graphics and the Media thread for playback of media.
    Using multiple threads allows JavaFX to make better use of now common multi-core processors.
    However, if you have a lengthy operation inside the event handling thread, using a separate thread is still necessary:

            ...
            // create content
            imageView = new ImageView();
            imageView.setImage(new Image(imageUrl, 241, 91, true, true, false));
            Button btn = new Button("Press me");
            btn.setOnAction(new EventHandler() {
                public void handle(ActionEvent ke) {
                    //UI will freeze as no events are handled, but at least no repaint
                    //mess as in Swing
                    Thread.sleep(1500); //InterupptedException handling omitted
                    //Rendering is done in separate thread
                    logo.imageView.setEffect(dropShadow);
                    logo.setLayoutX(190);
                    logo.setLayoutY(180);
                }
            });
  • JavaFX includes ways to very easily apply transitions and various effects. In the example below I add the reflection effect to an existing image object.
            Reflection reflection = new Reflection();
            reflection.setFraction(REFLECTION_SIZE);
            imageView.setEffect(reflection);
  • Easy property-binding. All properties of JavaFx-controls bindable. This means the values of two properties can be bound together, if one changes, the other one changes as well. You can for example bind the x-property of one control to the x-property of another control. Yet again something which makes a lot of coding easier. In the example below the property of bound the x and y property of a circle object to the logo object:
                circle.centerXProperty().bind(logo.layoutXProperty());
                circle.centerYProperty().bind(logo.layoutYProperty());

    If the logo moves, or more exactly said, if the x or y property changes, so will the circle. The binding is one-way in this case. If I’d use the method bindBidirectional, updates would go the other way around too. You can use expressions in bindings too:

             circle.centerXProperty().bind(logo.translateXProperty().add(10d));
  • Animation and other time-depending effects can be crafted using the Timeline-object. If something has to change over time, the Timeline works a lot easier than using threads, runnables, jobs or other hard to use concepts. In the example below I use the timeline to increase the radiusProperty of a dropshadow. The value is increased linearly from the start-value (in this case 0) up until 20 within a time-frame of 4 seconds. Again, no explicit thread-coding is needed, no need to set locks, use volatile or synchronized keyword or to do other cumbersome stuff.
                    KeyValue keyValue;
                    KeyValue keyValue = new KeyValue(dropShadow.radiusProperty(), 20d, Interpolator.LINEAR);
                    //create a keyFrame with duration 4s
                    keyFrame = new KeyFrame(Duration.valueOf(4000), keyValue);
                    //add the keyframe to the timeline
                    timeline.getKeyFrames().add(keyFrame);
                    logo.imageView.setEffect(dropShadow);
                    timeline.play()
  • Lots of standard-controls, amongst others for tables, buttons, trees and various charts. To quickly get an overview, run the Ensemble project. This project also includes very good documentation to get you started on JavaFX.
  • Swing and JavaFX can coexist. You can use JavaFX controls within Swing-applications. If you have a lot of Swing-based Java code, this can be a great plus. Maybe your Swing application does not even need to become a legacy application after all:
         JTable table = new JTable(tableModel);
            JTableHeader header = table.getTableHeader();
            header.addMouseListener(new SampleTableSorter(table));
            JScrollPane panel = new JScrollPane(table);
            panel.setPreferredSize(new Dimension(550,100));
            frame.getContentPane().add(panel, BorderLayout.SOUTH);
            // create javafx panel
            javafxPanel = new JFXPanel();
            javafxPanel.setPreferredSize(new Dimension(550,400));
            frame.getContentPane().add(javafxPanel, BorderLayout.CENTER);
            // create JavaFX scene
            Platform.runLater(new Runnable() {
                public void run() {
                    createScene();
                }
            });
            // show frame
            frame.pack();
  • Last but not least, JavaFX can be used with every language for the JVM! You’re not forced to write in Java, or Javascript for that matter. You can create JavaFX-application using JRuby, Scala or Clojure. I think quite a few of the features of JavaFX would fit very well in a (hybrid) functional language.
    If you want create rich internet application and you don’t want to code in Javascript or Actionscript, the JavaFX-framework helps you out.Here’s an example taken from JavaFX-site. If  I have some time left I’ll create an example in Scala.

    $time = Timeline.new
    $time.cycleCount = Timeline::INDEFINITE
    $time.keyFrames << KeyFrame.new(Duration.valueOf(1000),
        refresh_time_handler)
    sleep(1 - (System.currentTimeMillis()%1000)/1000.0)
    $time.play()
  • Browser integration – JavaFX applications run inside a browser, using the Java-plugin. The Java-plugin will be updated so the JavaFX-runtime is included, when JavaFX is no longer beta. Oracle invest time to make the installation and update of the Java-plugin even more easier. Furthermore most new desktop-plugins have the Java-plugin preinstalled.
    If Oracle makes the installation and update of the Java-plugin as easy as that of the Flash-plugin then Java on the client might return. The advantage of not having to test the application for every kind of webbrowser (since they all use the same plugin) could outweigh the disadvantage of having to use a plugin.

My try-out with JavaFX is available on GitHub too.
Javascript and HTML5?
Since the advance of HTML5 and various client-side Javascript frameworks such as JQuery and the new Canvas element control, creating advanced graphical applications in Javascript is possible too. However, when using JavaFX you’re not stuck with one language. You could create both the backend as well as the frontend in one and the same programming language of your own choice. Of course, the disadvantage is that you rely on a plugin in the browser.
Mobile and other platforms
The current beta is only available for Windows. Based from what I heard as well as the website, MacOS and Linux versions should arrive soon. For the time being, there’s no mobile version of JavaFX. Since JavaFX 1 was originally meant to be used on mobile phones, televisions, set-ops and lots of other platforms that’s a little bit weird.
Most mobile phones might lack hardware-accelerated graphics needed to use JavaFX, but many modern tablet-devices do have hardware-accelerated OpenGL support. My guess is, when the release-version of JavaFX 2 is out, versions for other platforms besides desktop-pcs and macs will follow soon.
Update at21:53 JavaFX will be available for other platforms besides the big three (Windows, Mac and Linux), according to the key developer.
Graphics
Last but not least, why would you care about programs with advanced graphics? Are existing web-frameworks such as Wicket, GWT, JSF, Liftweb or Rails not good enough? These frameworks all work very well to create CRUD-applications: software to display and enter data from and into a database using various kinds of forms and tables.
If you need to display enormous amount of data generated or contained in big-data systems, these frameworks don’t suffice. What if you have millions of records? You can’t create a scrollable table for that – you need much more advanced graphical applications. You could for example, use dynamic charts to visualize millions of transactions or allow a user to manipulate complex data by simply touching the screen or moving the mouse.
Conclusion
Oracle has invested quite some time and effort in JavaFX and from what I see now this is serious stuff. Hopefully MacOS and Linux versions will arrive soon as well. If you want to create rich graphical applications, JavaFX is worth checking out.

Questions?

Get in touch with us to learn more about the subject and related solutions

Explore related posts