Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java FX: declarative vs procedural

Tags:

java

javafx

I have a background in web-based programming (php, jsf,...) and minimal background with swing and swt.

Currently I'm looking at java fx 2.x for a new desktop application and I'm wondering about best practices concerning building the actual GUI. I could go the declarative route with fxml or I could go the procedural route. Currently for some quick prototyping I'm doing the latter but I was wondering whether there were compelling reasons to use fxml.

UPDATE

In the end I went the FXML route for an average-sized project and even though the scene builder beta is still somewhat unstable on my linux system, it has proven to be vastly superior to the original procedural prototype. By far the biggest advantage is that a lot of elements (especially hbox, vbox, labels, tabs,...) no longer clutter my code as they exist only in the fxml.

like image 205
nablex Avatar asked Apr 23 '13 05:04

nablex


People also ask

Is JavaFX deprecated?

JavaFX will be removed from the Java JDK as of JDK 11, which is due in September 2018. It is bundled in the current JDK 9 and will remain in JDK 10, due this spring. Commercial support for JavaFX in JDK 8 will continue through at least 2022.

What is JavaFX vs Java?

JavaFX is an open-source web platform that enables developers to create modern user interfaces for desktop, mobile, and browser applications. Java Swing is a GUI toolkit for Java, originally designed by Sun Microsystems. It is one of the most popular toolkits in the world.

What does FX mean in Java?

JavaFX stands for special effects in the Java language. Internet applications are made richer with different plugins with the help of JavaFX. This is an alternative to the ways for building a website or any other applications. We can say JavaFX is a declarative and statically typed scripting language.

Is JavaFX a framework?

JavaFX is an open source Java-based framework for developing rich client applications. It is comparable to other frameworks on the market such as Adobe Flex and Microsoft Silverlight. JavaFX is also seen as the successor of Swing in the arena of graphical user interface (GUI) development technology in Java platform.


2 Answers

Oracle Recommendations

See the Oracle advice:

  1. Why use FXML
  2. Implementing JavaFX Best Practices

Oracle do recommend FXML for layout definition over the Java API.

Alternate Declarative Technologies for JavaFX

The other declarative pieces in JavaFX are CSS and 3D models

A semi-declarative method is the JavaFX Builder API, but you might want to avoid that as the builder api will be deprecated in future JavaFX versions.

Additionally, if you program in other languages, some of these embed a declarative domain specific language (DSL) for JavaFX development, (e.g. ScalaFX or GroovyFX).

In general the use of declarative syntax has mainly won over procedural programming for the majority of UI markup tasks. This be seen by the prominence of technologies such as HTML, CSS, FXML, XAML, MXML, XUL, etc.

Low Level Programming

For low-level tasks such as developing a custom JavaFX control, manipulating a JavaFX canvas or processing image data, the procedural Java API is best suited rather than using declarative FXML - none of the JavaFX codebase in openjfx uses FXML.

Personal Choices and Advice

In the end, there is no right answer here. The choice is left up to the developer to choose the approach they like best.

There is also no reason why you cannot mix the two styles as you see fit. Using a straight declarative approach leaves you with a very rigid UI (such as a static html page), compared to something which mixes both procedural and declarative approaches (e.g. html + javascript + ajax) - and the same is true for JavaFX as it is for html development in this case.

For small programs I like just writing some code in the IDE, compiling and running it without having to deal with the context switch between XML based FXML and Java code. But I have found that this procedural only approach doesn't easily scale well to larger projects. Having the view separated into FXML helps to enforce separation of concerns and modularization. It is far too easy to mix these view and logic concerns up without the artificial separation which FXML demands.

I don't really like XML as a UI layout language. I think the now defunct FXD format from the obsolete JavaFX 1.x branch was far superior. However, FXML is the most accessible and widely used declarative UI syntax for JavaFX 2.

I use CSS with JavaFX programs a lot and like using it with both FXML declarative code and Java API procedural code. In my opinion, it is at least as important to separate style from code as it is to separate layout from code.

When using CSS, it is always best to put your styles in a separate style sheet rather than inline the styles in code.

As pointed out in another answer, the JavaFX SceneBuilder visual design tool currently only works with FXML and, all other things aside, that is reason enough for many to use FXML to define their JavaFX UI.

like image 110
jewelsea Avatar answered Sep 27 '22 17:09

jewelsea


My reason for using fxml is that it can be generated by Scene Builder. I don't want to fiddle layouts by hand...

like image 21
Kai Avatar answered Sep 27 '22 17:09

Kai