I am a Qt user since many years back using both C++ and Python for application development. I have recently had reasons to start looking at the JVM-platform and have been comparing GUI-toolkits. For Qt there is a Java-binding called Jambi, but it does not seem to be well supported.
So, I know about AWT, Swing, SWT and now JavaFX. Coming from a Qt background JavaFX feels both familiar and alien at the same time. I have studied some literature to understand how an application should be developed using JavaFX. In Swing there is a lot of class inheritance and method overloading. In Qt there are cases when classes need to be inherited and its methods overloaded and some cases where it is sufficient to set properties for proper behaviour. In JavaFX it is a bit unclear how to behave.
Anyway, the most difficult part (for me) to grasp when moving between GUI-frameworks is how to handle layout components. In JavaFX I have failed miserably to get layout components to behave the way I want to, even if the layout components are very similar in naming to Qt (and should do similar things). Sometimes components expand as they should and sometimes they don't. It feels like JavaFX layout is done backwards to the way Qt is doing it.
I would love to get some hints on how layout differs from Qt in JavaFX and how I should change my thinking.
Do you have any additional comments on the design of JavaFX when it comes to best practices for custom components?
So, what is the intention of the design of JavaFX regarding creating new classes through inheritance and overriding methods vs. modifying behaviour of existing objects by injecting lambda functions (instead of overriding)? My impression of Swing is that it is all about writing new classes and overriding. In Qt it is possible to take customization of widgets quite far by tweaking properties and more advanced behavioral changes are done by overriding methods. In which direction is JavaFX moving?
If my impression is correct that JavaFX is avoiding inheritance and method overriding using lambda functions in objects, what would be best practice according the JavaFX designers for building these components in a large scale application? Should I for example write factories which build the customized objects?
Joined: Jun 24, 2014
In most points your impression is correct. You can do a lot by using Lambda expressions. One of the best examples is how you can define custom cell renderers for tables and lists. Here you can define a renderer factory as a lambda callback. You will find this examples in my book.
But there are some cases where you still need to override methods. The compute... methods are such an example. In this case you have a dependency between width and height. This is another approach as you might now from swing. by doing so you can calculate the dimensions for labels with word wrap, etc. In this case you need to override the methods. If you only want a static size you can use the prefWidth property.
The CSS styling capabilities in JavaFX are very good, and the public control APIs have many properties/listeners/events, etc. which allows a great deal of customization of the controls that ship with JavaFX or are available through third party projects like ControlsFX. This makes those controls quite flexible and means that for many tasks, it is not necessary to develop your own custom controls.
Some JavaFX controls (e.g. WebView) are deliberately made final (as are some other JavaFX classes like Color), this makes for predictable behaviour for application developers and makes it easier for the JavaFX developers to develop APIs because they don't have to cater for unforeseen subclassing. It does mean that extension of some JavaFX functionality is done by aggregation or wrapping of existing controls and classes rather than subclassing. Not all classes are final though, so there is still the chance for subclassing in some cases if you wish to develop that way.
Stefan K Larsson
Joined: Jul 09, 2014
It might be similar to Apple's Cocoa framework then where no subclassing is done and all specialization is done through delegates.
John Damien Smith
Joined: Jan 26, 2012
My guess is that JavaFX coding probably sits somewhere in between the no inheritance model used in Cocoa (I've never used Cocoa) and the specialization by inheritance model in Swing programming. You can still specialize by inheritance in JavaFX, but it probably isn't as common as Swing due to the reasons I mentioned previously.