What’s new in Java 8 (Part I – JavaFX)

Disclaimer: I do not work for Oracle or represent Oracle in any way. This list of features is not official. This is just part of my research as an “outsider”.

Java 8 has become feature complete about two months ago and the developer preview is just around the corner (in a couple week’s time). This blog post will detail what’s coming in this next major release with an emphasis on JavaFX 8, which is the UI library. JavaFX release number is now on par with the Java release, that’s why it has jumped from 2.2 to 8.

This is part I of a three part series of posts. This first part will focus on what Oracle has officially been telling developers that should come out in JavaFX 8, the second part will be mostly speculative, and listing what I think might come out and is not officially being reported.  And the final part focuses on the rest that’s coming to Java 8, excluding the UI library (e.g., new language features and such).

Release dates

The original schedule aimed to ship the release in early September 2013, but due to the recent focus on browser-related security issues that date was not achieved. The new schedule is as follows:

  • 2013/06/13 – Feature Complete

All features have been implemented and integrated into the master forest, together with unit tests.

  • 2013/09/05 – Developer Preview

A reasonably stable build suitable for broad testing by the developer community is published. This build will include all planned features unless otherwise stated.

  • 2014/01/23 – Final Release Candidate

The date by which the final release candidate must be declared and submitted for testing. One or more release candidates will be declared after the planned ZBB date; if another is necessary after this milestone then the General Availability  date will be at risk.

  • 2014/03/18 – General Availability

Final release, ready for production use.

To this let me add that I’ve been developing Java based apps under Java 8 for more than a month now and consider it to be quite stable. I’m also not using any of the features under development (like 3D graphics for example), so I can’t say anything for that part.

You can already get an early access release of JDK8 here: http://jdk8.java.net/download.html

On that site you can also download some sample apps, where you can get a glimpse of what’s already possible to do.Ensemble8

New Features

The following list will focus especially on the most relevant upcoming features. The list of all coming new features and changes to existing ones is particularly big in this release and it could be that some relevant ones might have escaped my attention and may not me mentioned.

Rich text support

rich textRich text support will be added in JavaFX 8 via TextFlow class.

This will allow you to:

  • Style individual words;
  • apply effects to words
  • apply transforms
  • embed nodes inside text
  • support Bidi text, that is text containing text in both text directionalities, both right-to-left (RTL) and left-to-right (LTR)
  • create text that can be individually styled via CSS

For more details visit this links:

  • Rich text API Details:

  https://wikis.oracle.com/display/OpenJDK/Rich+Text+API+Details

Swing Node

Swing node will allow you to embed swing components inside a javafx scene. Support for the opposite, that is, embedding a javafx scene inside a swing app is already possible via JFXPanel.

For the API specification and a simple example visit: http://download.java.net/jdk8/jfxdocs/javafx/embed/swing/SwingNode.html

Changes to the controls API

Several changes will occur to the controls API, this is especially important to third party control providers:

  • Skin class which will become public;

SkinBase class will become public, but Behavior class and subclasses are still private implementation, there is still some work to be done here after JavaFX 8.

Video and audio recording

Support for video and audio recording will be added.

You can watch this video presentation for more details (jump to minute 39 to see an example app in action): http://www.youtube.com/watch?v=jaPUbzfJx2A

Update: Unfortunately this won’t be available after all. It has been postpone to a later release, code named “Van Ness” – https://javafx-jira.kenai.com/browse/RT-3458

Printing support

Printing support will be added to JavaFX. You can check this detailed blog post by Carl Dea for more details: http://carlfx.wordpress.com/2013/07/15/introduction-by-example-javafx-8-printing/

New looks with a new theme called Modena

Modena

The looks have been significantly improved with a new theme called Modena. This time a lot developer feedback was taken into account making for a very nice end result.

More details can be found here:

http://fxexperience.com/2013/03/modena-theme-update/

New DatePicker and TreeTable controls

JavaFX 8 will have a DatePicker and a TreeTable control:

  • DatePicker

DatePicker UXUser experience documentation: http://openjdk.java.net/projects/openjfx/ux/datePicker/

Actual DatePicker screenshot:

datePicker screenshot

Update: Jim Weaver has written about the DatePicker control, you can find his post here – http://learnjavafx.typepad.com/weblog/2013/08/quick-and-dirty-javafx-8-datepicker-example.html

  • TreeTable

TreeTableView

API examples: https://wikis.oracle.com/display/OpenJDK/TreeTableView+API+Examples

User Experience documentation:

https://wikis.oracle.com/display/OpenJDK/TreeTableView+User+Experience+Documentation

WebView Enhancements

The following enhancements were added to WebView:

  • Nashorn JavaScript engine (Update: I didn’t mean to say Nashorn is going to be webview Javascript engine. I’m just saying Nashorn will be available for use in Java8)

Nashorn’s goal is to implement a lightweight high-performance JavaScript runtime in Java with a native JVM. This Project intends to enable Java developers embedding of JavaScript in Java applications and to develop free standing JavaScript applications. (More on this on a later post).

More information on:

https://oracleus.activeevents.com/connect/sessionDetail.ww?SESSION_ID=4082&tclass=popup

Embedded Support

JavaFX will be included in Oracle’s Java SE Embedded 8. It will include a subset of features of the desktop version, namely it will not include:

  • WebView support
  • Media support

Workaround for media:

For media there is a workaround however, as this comment in FXExperience from Jasper Potts explains:

“There is no JavaFX media support on Pi, we prototyped passing
hardware decoded frames of video into OpenGL so that it could be drawn
in JavaFX Scene but performance was not great. So what we do is draw
JavaFX with a transparent background just like a transparent window on
desktop. Then use the native omx media player to play video to a lower
layer under the JavaFX graphics. So basically standard hardware overlay
graphics. This works great on PI and there is only about a 10% drop in
JavaFX performance when playing a HD video stream underneath. So simple
animations we can get 50+ fps with video at same time.”

(source: http://fxexperience.com/2013/08/javafx-hd-menus-on-raspberrypi/)

Improved 3D support

Improved 3D support will be available in this release. Or it’s probably more accurate to say: true 3D support.

It is an optional feature, you can query the runtime to know whether it is available for the given platform. When JavaFX runs with software rendering this 3D features will not be available.

·     Movable cameras and SubScene

  • Camera is now a Node

Camera can be added to a scenegraph, its position and aim (or orientation) is set using standard transforms.

  • Addition of SubScene

Subscene is a special node that can be used to render part of a scene with a different camera.

·     3D primitives

  • Added two type of 3D shapes, extending from an abstract Shape3D base class:
    • User-defined shapes (MeshView)
    • Predefined shapes

Three commonly used predefined 3D shapes are introduced: Box, Cylinder and Sphere.

Shape3D class Hierarchy:

  • javafx.scene.Node
    • javafx.scene.shape.Shape3D (abstract)
      • javafx.scene.shape.MeshView
      • javafx.scene.shape.Box
      • javafx.scene.shape.Cylinder
      • javafx.scene.shape.Sphere

Mesh Class Hierarchy:

  • java.lang.Object
    • javafx.scene.shape.Mesh (abstract)
      • javafx.scene.shape.TriangleMesh

·     3D Attributes

  • Added lights and 3D materials to add realism for 3D shapes.

Material specifies the appearance of a 3D shape. Light interacts with the geometry of a Shape3D and its material to provide the rendering result.

A Shape 3D can be rendered either as a filled shape or as a wireframe.

·     Light

  • Light is defined as a node in the scene graph
  • There are 2 types of light sources:

AmbientLight: A light source that affects all objects equally. AmbientLight objects model the light reflected from other visual objects. If you look up at the underside of your desk, you will see the bottom surface of the desk although no light source is directly shining on that surface (unless you have a lamp under your desk). The light shining up on the bottom surface of the desk reflected off the floor and other objects. In natural environments with many objects, light reflects off many objects to provide ambient light. The AmbientLight class simulates this effect.

PointLight: A light source with a position. The distance and direction to a given object affects how this lights up the object. PointLight objects approximate bare light bulbs, candles, or other light sources without reflectors or lenses.

May add more types of lights in the future (example: spot light, a light that simulates light sources such as flash lights).

  • A scene contains a set of active lights

A default light is provided when the set of active light is empty

  • Each light contains a set of affected nodes

If a Parent is in the set, all its children are affected. The default is the root node of the Scene.

·     Material

  • Material contains a set of rendering properties
  • PhongMaterial is a concrete subclass of Material. It has the following properties:
    • Ambient color
    • Diffuse color, diffuse map
    • Specular color, specular map
    • Specular power
    • Bump map
    • Self-illumination map

·     Methods added to Node

  • A LOD helper method that returns the area of the Node projected onto the physical screen in pixel units:

public double computeAreaInScreen()

LOD stands for Level of Detail. It is technique that changes the appearance of an object depending on how close or distant to the camera it is, or in other words the level of detail is changed according to the area occupied by the object if the object is far then the detail can be lowered without the user noticing it, and thus improving performance.

  • A new set of methods that transform 3D points

·       Loader support

Many 3D file formats exist, such as:  Obj, Maya, 3D Studio Max, Collada, KRML. There will be no official 3D file format loaders shipping with the API. Oracle however will provide sample code for one or two popular formats. At least a Collada file loader is in the works.

References

Other minor features, tweaks and changes

Builders are deprecated

Builders are now deprecated in this release. In Java 9 they will be removed (this is several years down the road). You should start planning to replace builders if you are using them.

Reason:

Due to an implementation detail, Builders would stop working in Java 8 unless some binary incompatibilities were introduced. Because of that the decision was to phase out the Builders, the changes that were required would reduce the value of the builders to the point where they just wouldn’t be worth it, and when considering Mobile / Embedded use cases, the extra cost of the builders would be prohibitive. (http://mail.openjdk.java.net/pipermail/openjfx-dev/2013-March/006725.html)

Javafx thread and swing will merge

JavaFX thread and swing can be merged, this is still an experimental API and will not be public, and you’ll have to turn it on explicitly. If you are developing a hybrid Swing/JavaFX app this will simplify the code since you only have to worry about one UI thread.

You can read more about this in this blog post:

http://wiki.apidesign.org/wiki/JavaFX

JavaFX fully open sourced

JavaFX has just recently become fully open sourced. This is great news since it will allow third parties and developers to extend the functionality of JavaFX more easily and thoroughly. Also good for debugging errors, and other things.

JavaFX on the default classpath

This has been a recurring request from developers, JavaFX classes are now on the default runtime classpath for an Oracle Java implementation.

Integrating JavaFX and Swing (Revised)

I’ve just finished rewriting a component of my app that was using Swing and now is using JavaFX, I’ve ended up with a JavaFX component that integrates with the larger swing app. It is a large app and the rewrite took me a while, in the end everything worked fine and I’m glad I did it.

Reasons you might want to do this in your swing app

You might want to rewrite your Swing app and change it to use JavaFX instead, the easiest way is to do this incrementally by changing each component at a time. This requires that you integrate each of the newly changed JavaFX components with the rest of your Swing app.

I’ll summarize why you might want to start rewriting your app from Swing to JavaFX:

  • It’s the future

Swing is pretty much dead in the sense that it won’t get any further developments. JavaFX is the new UI toolkit for Java, it is better prepared for the future with things like touch support, 3D, built-in animation support, video and audio playback, etc.

  • Probable future support for mobile: Android, IOS…

There is already a working prototype that enables you to port javafx apps to IOS called RoboVM – http://www.robovm.org/. As more and more of JavaFX gets open sourced the better RoboVM will get, with this open sourcing probably other utilities will arise that will enable ports to other environments.

  • It’s solid

JavaFX is a well-designed toolkit with a rapid growing pace, a bright future and a set of good free UI tools. Furthermore, unlike in the past, Oracle is giving developers feedback a great importance changing and adapting its APIs to meet their goals.

  • It’s pretty

Unlike Swing, not counting third party librarys, which was ugly by itself, JavaFX looks good right from the start, especially the new Modena skin coming to JavaFX 8: http://fxexperience.com/2013/03/modena-theme-update/– . Given that users nowadays expect good looking, well designed apps this is a pretty good point.

  • Nice extras

Some nice extras, like the charts API, an embedded browser that supports HTML5, etc.

How you do it

Back on JavaFX 1.3 you could embed Swing in JavaFX but not the other way around, at least not officially. I implemented a Swing component that allowed you to embed JavaFX content in Swing (called JXScene) and made it publicly available in the jfxtras project. It was the only way you could embed a JavaFX scene in a Swing app.

Now Oracle with JavaFX 2.X made an official way of embedding JavaFX in Swing which makes more sense but unfortunately not a way to embed Swing in JavaFX, I guess this will suffice in most cases. However, with the coming JavaFX 8 you’ll also have the ability to embed a swing component in a JavaFX app with the Swing Node.

ARQUITECTURE

Essentially when you are embedding JavaFX in Swing you end up with 2 running UI threads: the Swing EDT thread and the JavaFX User thread.

There is a chance that in the future there will only be one thread for both as is the case with SWT, making Swing run on the JavaFX User Thread, but for now we’ll have to manage our way with 2 threads.

Two threads running at the same time in the UI is what complicates matters, and makes JavaFX integration not as easy as you might expect, unless you’re doing some trivial small app but I guess that is not the scenario for most of the real world use cases. If you’re doing a small app might as well do it all in JavaFX.

CODING

JavaFX gives you JFXPanel, which is a Swing panel that hosts a JavaFX scene. You set the scene on the JFXPanel and add the panel wherever you could add a Swing Component.

To access JavaFX data you have to wrap your code in a Runnable object and call the Platform.runLater method:

jbutton.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
        Platform.runLater(new Runnable() { 
            @Override
            public void run() {
                fxlabel.setText("Swing button clicked!"); 
            }
        });
    }

});

On the other side is Swing data. This data must be accessed only by the EDT. To ensure that your code is running on the EDT, wrap it into a Runnable object and call the SwingUtilities.invokeLater:

SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        //Code to change Swing data.
    }
});

Tips

  1. JavaFX already throws exceptions when you access a JavaFX resource outside the JavaFX User Thread, but bear in mind that this does not happen always. To minimize performance costs not all situations are checked.
  2. If you use Substance (in my opinion without any doubt the best looking free look and feel for swing) third party library than an exception will also be thrown whenever a Swing resource is accessed outside the EDT. Setting Substance as your Swing look and feel might be a good solution to lessen concurrency mistakes on the swing side that you might do.
  3. Be very careful when sharing resources between the 2 UI threads, try to avoid this as much as possible. Best way to solve multi-threading problems is to avoid them, and these kind of problems are among the most difficult to solve in Software Engineering. There is a reason why Swing started off as a multi-threaded toolkit and ended changing to a single threaded one.
  4. Sometimes you might want to check if you are on the JavaFX User Thread via Platform.isFxApplicationThread() and only than issue a call to Platform.runLater(…), because if you are on the JavaFX User Thread and call runLater(...) the execution of the code that is inside will still be deferred to a later time and this might not be what you want.
  5. There are a lot of JavaFX controls that cover their swing counterpart, however they are different and have different features that you must adapt to. There are also some controls like the JFormattedTextField that don’t yet exist. In conclusion JavaFX is different from Swing. Has different controls and a different arquitecture and API that you must adapt to.
 Other links to check out:

Metro style Spinner for Java (JMetro)

(The design of the Spinner has been refined and implemented in the JavaFX Spinner library control. Check this post and the JMetro page).

Here is Tom Eugelink’s spinner control from JFxtras with a metro style.It’s basically a control that allows the user to iterate through a list of values using an increment and decrement button.

I’ve changed the code on ListSpinner so you’ll have to use my version in order for the metro style css to work.

A sample app is included in the files.