Patrick Forhan created an excellent, concise, JavaFX tutorial on PlanetJFX entitled How to build your first JavaFX application.
It will serve two purposes to readers of this blog:
- You'll get an excellent review of some of the concepts that you've learned to this point.
- You'll be introduced to the concept of JavaFX triggers, which are a very powerful construct that help enable the declarative programming model of JavaFX.
Please go ahead and take the tutorial, running each step in your JavaFX Script tool of choice (please see previous posts for some available choices). I'll meet you back here when you're finished.
One of the features of JavaFX that makes declarative scripting work well in conjunction with classes is the concept of a trigger. A trigger, as you would expect, is run automatically when a particular condition occurs. In the tutorial that you just experienced, a replace trigger is used to print the value of an attribute whenever it changes. Replace triggers also very useful for protecting the value of an attribute. In the declarative programming style of JavaFX Script, instead of using setter methods to alter the value of the attribute of an instance, we tend to just assign the new value and let the trigger protect the state of the attribute. For example, the code in the replace trigger on the
minutes attribute of a hypothetical
Time class could protect the
minutes from being set to less than 0 or greater than 59.
There are other triggers in JavaFX that we'll get into later in this blog, mostly relating to sequences (arrays), with which you can cause code to execute when, for example, an element is deleted.
Regards, and a special thanks to Patrick Forhan for his excellent tutorial featured in this post,
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications