JavaFX looks promising!
I want to say "Amazing", but there are some hic-ups that still bother me yet with it, at least to the extent of my understanding of the JavaFX framework. As something that has only been announced recently, there is very little information about it still, however I gather my knowledge from the official site's Language Reference, because that is what I'll be writing about today (and not the cool animations and fade-outs you can do with it, not even about the Motorola site recreated using JavaFX)
So, just to summarise it for those two haven't been paying attention, Sun has announced a competitor to Flash, even though that's not what they're saying. They are saying, though, that it is a tool to easily create rich graphical user experience, which will leverage Java's key feature: Write-once-run-everywhere. That means that Sun aren't just bringing into the field another Flash competitor - They are bringing it everywhere, and they're starting with mobile phones.
Now to the features of the language. JavaFX is nothing like Java. First, it's a scripting language aimed at rich graphical experience, and not for business workflow management. Second, it's not type safe in almost any fashion possible - with the advantages and disadvantages of that. There is
var, and it infers its type from its use.
These two factors, I believe, are key to the additions in this feature-savvy scripting language, and I will enumerate the ones I like best:
- Do / Do Later: Forget about threads. Or at least, forget about the
java.lang.Threadclass. Want to start a thread of your own, and not worry about creation of aRunnable, termination and the likes?dowill do it for you. Just start adotag, write some code in it (don't forget to not access any AWT members!), and viola! And if you want to access some AWT members in it (something you would have done using thejava.awt.EventQueue.invokeLatermethod), just open ado later! - SQL-like selection: Complicated
if..elsestatements, nestedforloops, and other means of incomprehensible code? The new SQL-like selection can be found almost anywhere an array can be found. Some examples: Loop through all the albums I really like:for (album in albums where album.stars == 5)Retrieve an array of all albums containing tracks I hate:select album.name from album in albums, track in album.tracks where track.stars < = 2 - Lists of numbers: You can now create a list of numbers with any incremental value you choose with a simple command. The default incremental value is 1, but it can be anything with a simple syntax where you define the first element, the next element, and then the final element. The incremental value will be deduced as the difference between the first and the next, and the list will be filled up with values until the last element is reached. Examples:
Numbers between 1 and 100:
[1..100]All odd numbers between 1 and 10:[1,3..10]Numbers between 1 to 10, going backwards:[10..1] - XPath-like selection: Same as before, but XPath style. Example:
var list = [1..100]; var oddNumbers = list[n|n % 2 == 1] - Triggers: Events and listeners have been a long time design pattern, and are used in almost any application, especially a graphical user interface, where a view must reflect the correct information contained within the data model. Usually, a lot of boiler-plate code is being written with
Listenerinterfaces,fireEventmethods, andonFiredEventimplemented in concrete classes. In JavaFX, not only you can write anonymous procedures to answer events such asonMousePressed, you can also hook into four automatically generated events for classes, attributes and arrays.trigger on new Xwill allow you to apply code to the event of a new instance of type X.trigger on insert i to arr,trigger on delete i from arrandtrigger on arr[i] = newValallow you to apply code to the event of changing an array in any fashion. In addition, the replacement trigger works for any attribute of a class, sotrigger on x.name = newValwould also allow code in the event of a change to thenameattribute in thexinstance. These allow to modify a complex model, and to update a view when a model has changed internally, with much less code than we're used to see. - Binding attributes: There was a lot of work done on attributes (to avoid confusion between languages, these are the items you would usually
getandset). To simplify the mainstream use of triggers, JavaFX allows for relationships between attributes and variables or other classes' attributes in two ways: Thebindmechanism helps track changes to another attribute or variable, in a way that whenever a change to the binding target is made, the bound item is changed immediately. For example, iftextBox.textwas bound tox.name, any change to model (x'sname, in this example) would be reflected in the view (thetextBoxitem). The binding can also be done lazily if the attribute is not accessed often and it would be a waste to keep updating it. The syntax is simple:name: bind [lazy] textBox.text. Note thatlazyis optional for lazy binding. The other type of relationship in attributes is parent-child relationship. You can define in your code the parent of an item (i.e. the array containing it). This removes quite a lot of code which keeps the containing parent array in sync with the values of the class' children. And it's done in a simple way:attribute parent: Person inverse Person.children.
Comments (4)