Code-Dasher, anyone? An example of JavaFX
May 12

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.Thread class. Want to start a thread of your own, and not worry about creation of a Runnable, termination and the likes? do will do it for you. Just start a do tag, 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 the java.awt.EventQueue.invokeLater method), just open a do later!
  • SQL-like selection: Complicated if..else statements, nested for loops, 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 Listener interfaces, fireEvent methods, and onFiredEvent implemented in concrete classes. In JavaFX, not only you can write anonymous procedures to answer events such as onMousePressed, you can also hook into four automatically generated events for classes, attributes and arrays. trigger on new X will 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 arr and trigger on arr[i] = newVal allow 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, so trigger on x.name = newVal would also allow code in the event of a change to the name attribute in the x instance. 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 get and set). To simplify the mainstream use of triggers, JavaFX allows for relationships between attributes and variables or other classes’ attributes in two ways: The bind mechanism 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, if textBox.text was bound to x.name, any change to model (x’s name, in this example) would be reflected in the view (the textBox item). 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 that lazy is 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.

With all these great features, JavaFX brings some confusion too. There are a lot of new features that I do not understand still (such as the Extent feature), some that I understand but I know will be hard to wrap my head around (such as the arrays being all flat) and some lacking features that seem important to me (like JIT and primitives for floating point numbers). I also didn’t talk about the new way to create objects, and specifically graphical elements. I will make a separate post about that, and if I’ll see it fits about a comparison between this framework and two other frameworks I follow, the WPF (in .NET) and Cocoa (in Objective-C, for Macintosh applications development).

In the meantime, go ahead and start playing with JavaFX!

Other posts of interest

4 Responses to “JavaFX looks promising!”

  1. arun Says:

    Hi,

    I tried to install the JavaFX plugin on Eclipse 3.2 and Netbeans 5.5 IDEs but in both cases the servers were not detected. Can you lead me to the manual download of the plugins so that I can configure them on to eclipse or Netbeans IDE.

    Thanks !

  2. Avah Says:

    Did you follow the installation instructions on the openjfx.org site and were not successful in installing the plug in?

    I didn’t try any of the plug ins myself; I used the JavaFX Pad available on the main page as well to toy around with the platform. If you did try the installation instructions available on the site, let me know and I’ll see what I can do.

  3. Chaotic Java » Blog Archive » The confusion JavaFX brings Says:

    […] In my earlier post I mentioned that JavaFX had some features I couldn’t understand or didn’t like all too much, and also promised to talk a bit about the new method JavaFX has for instantiating elements. […]

  4. JavaFX articles at “Chaotic Java” blog | JavaFX site Says:

    […] JavaFX looks promising! […]

Leave a Reply

Chaotic Java is Digg proof thanks to caching by WP Super Cache!