How does JavaFX compare to WPF?

WpfJavafx 2Javafx

Wpf Problem Overview


I'm mostly a C# programmer, I stopped writing Java about 10 years ago, but I try to keep up with the technology in Java by reading articles, talking with friends, etc.

I've heard about the new rich GUI framework called JavaFX, but couldn't find any resource comparing it with non-Java parallels.

Since I'm very familiar with C# and WPF, I'd like to get a feel about how similar or different the two technologies are.

EDIT: Seeing as there's no answers coming, I'll try to be more specific:

  1. WPF uses XAML to create the visual tree, does JavaFX have something similar?
  2. WPF is best used with binding to a view model in an MVVM pattern, does JavaFX also make extensive use of binding?
  3. WPF utilizes the GPU for rendering, does JavaFX do the same?
  4. How does Silverlight compare to JavaFX when run through a browser on a net pc?

... More to come...

I'm changing this to community wiki so that the comparisons can keep getting updated (hopefully).

Wpf Solutions


Solution 1 - Wpf

I have been learning JavaFX for the last couple of weeks. Here is a high level overview of how it compares to WPF in my eyes:

All of my comments are related to JavaFX 2.0. This information will probably be subject to change as the platform is still fairly immature and is being actively developed.

Graphics

Like WPF, JavaFX uses a retained graphics rendering system. The user interface comprises a scene graph which is composed of 'nodes' which can be thought of as conceptually similar to WPF's UIElement.

JavaFX will offload the graphics rendering to the GPU if available. The graphics system uses DirectX on Windows and OpenGL on other platforms.

Markup

JavaFX user interfaces can be created both in code and via FXML markup which is similar to XAML in that the object graph can be created by nesting elements.

FXML has some similar features to XAML such as property binding (simple expressions only) and binding to event handlers (any onEvent method). Event handlers can be declared in-line but typically you would bind to an event in the associated controller.

FXML files can have an associated controller which allows you to declare complex event handlers and to set up bindings between properties. This is a controller in the MVC sense and is not the same as a viewModel in the WPF world (typically a controller will have references to nodes and controls).

One difference to WPF is that it appears that the FXML is not compiled into an intermediate binary representation like BAML. I haven't noticed any performance issues yet but have not used the system extensively. I have noticed though, that FXML usually tends to be shorter than any XAML as the platform still encourages you to write code and styles are declared separately.

An introduction to FXML can be found here.

A scene builder is provided free (as in beer), so if you don't like hand coding the UI you can drag and drop elements, set properties and bind to code in your controller and the FXML will be generated automatically. Obviously the scene builder is nowhere near as powerful as Expression Blend but it is still better than the 'designer' provided by Visual Studio.

Binding

JavaFX has a very powerful property and binding system. The Java Bean pattern has been extended to include classes that encapsulate a property (similar to the way WPF dependency properties represent properties). These classes implement interfaces that provide invalidation and change notification.

There is a distinction between invalidation notifications and change notifications. Invalidations just tell you that the binding expression is now invalid and needs to be recalculated; the recalculation does not actually occur until you request the property value via its get() or getValue() methods. If you have registered a change listener, however, then the expression will be re-evaluated immediately and anything that is bound to that property will reflect the changes.

JavaFX exposes these properties in a similar way to WPF with a get and set property and a method that returns an instance of the property wrapper (which are not static like WPF properties).

Complex bindings can be created between multiple properties. Want an integer property to be the sum of two others (a = b + c)? No problem, JavaFX provides a Fluent API to express these kind of relationships E.G.

> A.Add(B, C);

If the value of either B or C changes then the appropriate notifications will be raised so that the system knows that A needs to be re-evaluated. Note that in this case, an exception will be thrown if you try and set the value of A as it is bound to the other properties so it does not make sense in this context.

These expressions can be fairly complex E.G. a = (b + c) * (d - e) and can include any number of properties. The fluent API is fairly easy to read and use but is not as nice as some of the Fluent API's provided by some of the Microsoft libraries but this is more down to the Java language limitations rather than JavaFX itself.

Simple bi-directional bindings can be created between properties of the same type so that if one is updated the other automatically reflects the change.

JavaFX also provides a low level API to customise bindings yourself if you want to create a custom binding expression that is not provided by the API or if you are concerned about performance.

One of the biggest differences between JavaFX and WPF is that bindings are primarily carried out in code in JavaFX vs. the WPF way of establishing bindings in mark-up.

An introduction to properties and bindings can be found here.

Styles

JavaFX uses CSS to change the looks of the nodes contained in the scene graph. There is a full specification available which explains the types and the properties that can be set on each node type.

JavaFX also provides some additions that help to improve CSS such as variables that can be defined and used elsewhere E.G.

.button {
    my-custom-color: RGB(234, 44, 78);
}

.my-control {
    -fx-background-color: my-custom-color
}

It also provides a couple of functions that allow you to derive colours from other previously defined colours which is useful for creating things like gradients. This means a base palette of colours can be defined and the rest can be generated from these values (this is what the default JavaFX CSS file does).

JavaFX CSS does not allow you to define the type of layout used by a node (as of writing this all layout needs to be performed in code). This works really well for me as this was the one aspect of CSS that really caused me pain when using it with HTML.

Personally I prefer CSS to XAML styles which tend to be too verbose for my liking.

A guide to JavaFX CSS can be found here.

Layout

JavaFX provides a number of layout panes that are similar to those provided by WPF. One difference I have noticed is that the measure and layout contract is defined further up the inheritance chain in the Region class.

As previously mentioned, Layout cannot be carried out using CSS but can be expressed using the code, FXML or created using the scene builder (which is ultimately converted to FXML).

Controls

JavaFX provides an ever growing library of controls that we have come to expect. One major difference between JavaFX and WPF is that the controls are essentially black boxes and cannot be re-templated in the way that WPF controls can. They also seem to expose far less properties than the WPF controls.

The controls do expose some of the implementation specific regions to CSS allowing specific areas of a control to be targetted by your styles. This is known as the substructure of the control. E.G. a CheckBox exposes two substructures; the box and the check mark allowing each part of the control to be styled independently. Note that as described earlier only the look of a control can be altered using CSS but the feel cannot. E.G. you cannot dramatically alter the way a TabPane lays out its content by altering its internal layout panel in the way you can with the WPF TabControl.

Whilst this sounds fairly limiting, the preferred way of creating custom controls in JavaFX seems to be using composition along the lines of deriving from a layout panel to position standard controls and re-styling them using CSS.

Conclusion

Overall I am very impressed with what JavaFX has to offer at the moment. Whilst it is no where near as mature as WPF it is being actively developed and Oracle certainly seem to be backing this. Time will tell if it's successful or not.

I would recommend giving JavaFX a try. Read the documentation and try putting together a small application and see what you think.

You should also check out FXExperience.com which is updated regularly with information from the development team.

Solution 2 - Wpf

I think the best way to to get a feel for JavaFX is to just try it out. There are some good tutorials on the JavaFX website. Here's a couple:

They are pretty quick and give you a good feel for the language. There are many others on the JavaFX site if you are interested in more tutorials and articles.

For specific answers to your questions:

  1. JavaFX has it's own declarative language for creating the "visual tree" which is not an xml derivative. The UI is based on a scene graph so you can apply various effects and animation to any node in the graph. See the tutorials for more information. There is also a designer tool for JavaFX (which I have not yet tried).
  2. JavaFX has binding built into the language.
  3. JavaFX on the desktop uses Java AWT/Swing which does use GPU rendering. Every version of Java seems to offload more of its graphics to the GPU. Chris Campbell from Sun has blogged some about GPU acceleration. I'm not sure if the mobile version of JavaFX has GPU acceleration. I found that earlier versions of JavaFX weren't performant enough for what I needed, but I know the latest version does have significant performance improvements over previous versions and they are still working on making it faster.
  4. JavaFx uses Java Applets to run in the browser. As of Java 6 update 10, the Java applet framework has been reworked and though it is not as seamless as Adobe flash, it is much improved. I'm not sure how it compares to Silverlight other than I've had trouble getting Silverlight to work on Linux, but did get JavaFX working on Linux.

Here's another related question.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAviad P.View Question on Stackoverflow
Solution 1 - WpfBenjamin GaleView Answer on Stackoverflow
Solution 2 - WpfJay AskrenView Answer on Stackoverflow