aspose file tools*
The moose likes JavaFX and the fly likes Qt vs. JavaFX (layout) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JavaFX
Bookmark "Qt vs. JavaFX (layout)" Watch "Qt vs. JavaFX (layout)" New topic
Author

Qt vs. JavaFX (layout)

Stefan K Larsson
Greenhorn

Joined: Jul 09, 2014
Posts: 10
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.
Hendrik Ebbers
author
Ranch Hand

Joined: Jun 24, 2014
Posts: 32
    
    6
The layout mechanisms of JavaFX are different to the one I know from Swing and other UI Toolkits. You can find some layout basics of JavaFX in this blog post: http://www.guigarage.com/2012/11/custom-ui-controls-with-javafx-part-2/
In addition my book contains a complete chapter about this topic: http://www.guigarage.com/javafx-book/


[Mastering JavaFX 8 Controls]
Stefan K Larsson
Greenhorn

Joined: Jul 09, 2014
Posts: 10
Thanks, the links explained some things well.

Do you have any additional comments on the design of JavaFX when it comes to best practices for custom components?

In your blog (part 2 about custom components) there are some methods which needs to be overridden, like computeMaxHeight (example from your blog post). But for listeners for properties it is enough to add a lambda function (like someProperty().addListener(observable -> doSomething())). When reading the JavaFX documentation I get the impression that the designers wants to avoid overriding classes and modify behaviour of objects by injecting lambda functions instead. This is something which has been easy to do in e.g. JavaScript and Python for a long time and the languages give you enough freedom to choose which approach you want to use.

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?
Hendrik Ebbers
author
Ranch Hand

Joined: Jun 24, 2014
Posts: 32
    
    6
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.
John Damien Smith
Ranch Hand

Joined: Jan 26, 2012
Posts: 145
    
    9
In terms of styling and customizing existing controls there is reasonable summary of how to customize controls for JavaFX 2 here:
http://stackoverflow.com/questions/12383533/javafx-2-0-style-template-existing-control

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.

JavaFX 8 adds additional customization possibilities over JavaFX 2 by providing public API to create custom skin code for controls, but often you don't need to go to that extent. If the concept of a skin is foreign, you can read up on it here:
https://wiki.openjdk.java.net/display/OpenJFX/UI+Controls+Architecture

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
Greenhorn

Joined: Jul 09, 2014
Posts: 10
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
Ranch Hand

Joined: Jan 26, 2012
Posts: 145
    
    9
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Qt vs. JavaFX (layout)