20 September 2009

Ten Interesting Aspects Of JavaFX Script

With the JavaFX Script programming language being cited by many as one of JavaFX's main strengths it is time to look at the language's interesting aspects. Some aspects will pleasantly surprise you, while others will come as a shock. Here are the 10 interesting aspects of the language.


1. Built-in support for generators

Comes in handy for generating multiple items. In particular it is commonly used for generating multiple nodes for use in layouts. Each generated node may be customized differently to accommodate different data being supplied for display.


2. Can run actions at a later time

Useful for displaying splash screens properly when there is some background processing going on. Below is some sample code that demonstrates this (in a script):

// Splash screen is displayed here.

public function run(args: String[]): Void
{
FX.deferAction(doBackgroundProcessing());
}

function doBackgroundProcessing(): Void
{
// Background processing is done here.
// Display the main screen, and then close the splash screen.
displayMainScreen();
}

What is important to remember here is that the function you are passing to the deferAction function must have a return type of Void. The deferAction function allows the splash screen to be fully displayed, and we can have the main screen displayed just after the splash screen is closed.


3. Can format dates in strings

Some concise syntax is provided by JavaFX Script to enable you to embed date formatting in strings.
All of the date formatting options in the SimpleDateFormat class can be used, for example:

var formattedDate = "{%MM/dd/yy java.util.Date{}}";

In the above code a % char is used followed by the date format and a Date object to format a date in an expression.
It may be possible in future JavaFX releases that regular expressions can be used as a string formatting option.


4. No support for static functions, variables and constants

An exception to this is with scripts, not classes. It would be interesting to know why JavaFX Script has no static support (an important design decision that was made?). Someone from the JavaFX development team would be able to shed more light on this.


5. Basic support for lookups (on nodes only)

Very handy when you do not want to keep a variable around, and want to temporarily access a node in order to run an action on it or make some changes. It may be possible that lookup support will be expanded to other areas (more general support) in future JavaFX releases.


6. Function parameters are immutable

At first this will be strange for those that have not done functional programming before. Since JavaFX Script has built in support for functional programming this makes sense. By making function parameters immutable it means that a function can safely perform operations on the parameter, without changing it thus when the program runs no funny side effects will occur.


7. Built-in support for mixins

Mixins are like interfaces except that you can define behavior in functions. Since mixins replace multiple inheritance it has made it easy to have a mixin or class inherit from multiple sources (mixins) in a predicable (clean) way. Do note that a mixin cannot be initialised.


8. First class internationalization support

This is one of the best features of JavaFX Script since it makes it a piece of cake to internationalize a program. Some unique syntax is used to make it easy to internationalize strings which hook up to a fxproperties file, for example:

Stage
{
title:
##"Window Title"
scene: Scene{}
}


The title property's value is tied to a key called "Window Title" which is prefixed by ## (indicates an internationalised string). Here is the corresponding resources_en.fxproperties file that contains that key:

"Window Title" = "Main Window"

Some people would consider this to be another form of lookup in JavaFX Script. It is very much up to debate if this is the case.


9. Built-in support for animation

It is much easier to handle animation when it can be done directly without the need for a library (less abstraction), and most properties can be manipulated via binding for use on a timeline. The unique JavaFX Script syntax provides a concise way to animate nodes quickly in a scene graph.


10. Built-in support for binding

Makes it very easy to keep properties in sync, thanks in part to the unique JavaFX Script syntax. Even better is the fact that you do not need to create any tedious, and error prone boiler code to do it which helps to reduce overall maintenance.


Some of the formatting of this blog post is incorrect since I am unable to get the editor to properly change the formatting (major bug). Hopefully this will be resolved in time for my next blog post.

9 comments:

  1. Nice!!!

    missed some things like triggers!!

    ReplyDelete
  2. Just to clarify deferAction() doesn't cause the action to be executed on a background thread, it just says 'run this function 'later' on the Event Dispatching Thread'.

    And in my experience, 'almost later' is pretty much immediately.

    If you want to run something in the background, you need to look at javafx.async.Task

    ReplyDelete
  3. It is nice blog,Thanks for sharing resources related to website design

    ReplyDelete
  4. Please, anyone,say Josh Marinacci, his program at javafx.com has an error!

    Program which calculate prime factors.

    The incorrect part interacts with java threads.

    2 is not counted and the result is less than correct -> 78497 vs 78498

    I wrote him to joshy@joshy.org , but no answer...

    ReplyDelete
  5. All fair points apart from the bit about XML. The widespread use of XML alongside Java is precisely because Java is so bad for programming in a declarative style. JavaFX fixes that. Why would you use an irritatingly verbose document format to write programs instead of a purpose-built declarative programming language? His trivial example looks fine but doesn't contain any code to perform actions, only declarative layout code. How would he propose to encode that in XML? XML makes a very bad programming language as anyone who's worked on a moderately compliated Ant build will testify.

    battery

    ReplyDelete
  6. Hi,
    really nice post, but I have to point one huge mistake.

    "4. No support for static functions, variables and constants"

    Of course there is support for static functions and varables in JavaFX. True - there is no "static" key world but it is even simpler then that. If you would like to make function/variable/const static you just put them outside the class declaration.

    public class MyClass{
    public function memberFunction(){
    }
    }

    public function staticFunction(){
    }

    var staticVar;

    def STATIC_CONST;


    and thats it. You can call/access them with:

    MyClass.staticFunction();
    MyClass.staticVar;
    MyClass.STATIC_CONST;

    Regards!
    Michal

    ReplyDelete
  7. Good point Michal. I recently discovered what you described, at first it was odd with the way static functions, variables, and constants are done in JavaFX Script. However like you say it is easy to make something like a function static.

    Also it does seem natural to do it that way instead of marking it with the static keyword. You can more easily tell what is static and what is not since there is a clear separation between the two.

    ReplyDelete
  8. I have a problem in the javafx composer.I have a class called Count in which I made a variable var temp=60; & I have another class called Gui in which I want to call the variable temp..so can I know how please ??

    ReplyDelete
  9. some of the main interesting aspects you mentioned here were told by my buddy Sildenafil Citrate who is an expert in this particular field and I did not understand so much, but I can see you two have similar thoughts

    ReplyDelete