Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
706 views
in Technique[技术] by (71.8m points)

javafx - Java FX: declarative vs procedural

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...