Java FX: declarative and procedural

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

I am currently looking at java fx 2.x for a new desktop application, and I am interested to learn about the best practices for creating a real GUI. I could skip the declarative route with fxml, or I could go through the procedural route. I'm currently doing the latter for rapid prototyping, but I was wondering if there are good reasons to use fxml.

UPDATE

In the end, I went on the FXML route for a medium-sized project, and although the beta version of the scene builder is still somewhat unstable on my Linux system, it turned out to be significantly higher than the original procedural prototype. The biggest advantage is that many elements (especially hbox, vbox, labels, tabs, ...) no longer clutter my code, since they exist only in fxml.

+8
java javafx
source share
4 answers

Oracle recommendations

See Oracle tip:

Oracle recommends FXML for layout definition by Java API.

Alternative declarative technologies for JavaFX

Other declarative parts in JavaFX are CSS and 3D models.

The semi-invariant method is the JavaFX Builder API , but you can avoid this as the api builder will be deprecated in future versions of JavaFX .

In addition, if you program in other languages, some of them embed declarative domain language (DSL) for JavaFX development (for example, ScalaFX or GroovyFX ).

In general, the use of declarative syntax has largely benefited from procedural programming for most user interface markup tasks. This can be seen from 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 better than using declarative FXML β€” none of the JavaFX codebase in openjfx uses FXML.

Personal choices and tips

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

There is also no reason why you cannot mix the two styles on your own. Using a direct declarative approach leaves you with a very tough user interface (like a static html page) compared to mixing both procedural and declarative approaches (like html + javascript + ajax) - and the same is true for JavaFX, since it is for html development in this case.

For small programs, I like to just write some code in the IDE, compile and run it, without having to deal with the context switch between the FXML XML interface and the Java code. But I found that this procedural approach does not just scale well for large projects. Having a view divided into FXML helps ensure separation of problems and modulation. It is too easy to mix these views and logic without the artificial separation that FXML requires.

I don't like XML as an interface layout language. I think that now the nonexistent FXD format from the legacy JavaFX 1.x branch was much higher. However, FXML is the most accessible and widely used declarative user interface syntax for JavaFX 2.

I often use CSS with JavaFX programs and like to use it with both the conditional code FXML and the procedural code Java API. In my opinion, it is at least as important to separate the style from the code, since it is intended for layout layout from the code.

When using CSS, it’s always better to put your styles in a separate stylesheet rather than embed styles in your code.

As already mentioned in another answer, the JavaFX SceneBuilder visual design tool currently only works with FXML and, among other things, is a good reason for many to use FXML to define their JavaFX interface.

+12
source share

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

+2
source share

If you separate the layout (FXML) from the application code, the likelihood is that you can ask some UX guys to work with the layout using SceneBuilder, instead of requiring them to have an entire IDE.

+2
source share

However, SceneBuilder is still not enough to work with it. We have to go back to FXML in the hand for details, and that is not good.

And a small mistake in SceneBuilder with a large and detailed FXML file.

Oracle Must Update SceneBuilder for JavaFX Health! Because a very good tool!

+1
source share

All Articles