Friday, 11 September 2009

JavaFX Week 3.1 New knowledge (and broken examples)

I would like to say a big thank you to all those people on the JavaPassion, JavaPosse and Sun JavaFX forums for posting their feedback on their respective sites. It was ultra-cool to get some comments from Tor Norbe from the JavaPosse who I have been listening to for years - with some valuable feedback (see my comments on the previous blog for the link).

Additionally a nod to Russell Miles, an experienced developer and author of many books, but more importantly a friend, he has blogged a response to my 'first impressions of JavaFX' post. He didn't agree with all my points, but I would like to put him straight on one thing - JavaFX is statically typed, evidence is here from the horses mouth...

Source: Learning JavaFX script PDF resource (By Robert Eckstein and the Authors of the JavaFX Programming Language Reference, July 2007) "The JavaFX Script programming language (hereinafter referred to as JavaFX) is a declarative, statically typed scripting language from Sun Microsystems, Inc."

My blog today covers some interesting points from that documentation...

JavaFX is primitive

(Primitive types)
The JavaFX programming language provides only four primitive types: String, Boolean, Number, and Integer. Note that, unlike the Java programming language, primitives are written with an initial capital letter! So I guess that's something along these lines

JavaFX Java equivalent 
String java.lang.String
Boolean boolean
Number floats and doubles
Integer byte,short, int, long

But you can fully qualify the Java type to get a Java Boolean, if that's what you really, really want.

// var b : Boolean = null; <-- does not work
var b: java.lang.Boolean = null;

The feedback I received on the JavaPosse was that using nulls is BAD BAD BAD, it makes some sense when you are using objects that map on to database objects - but even that apparently is bad practice, a boolean that's null to me implies 'not known', so I think it's okay - the user might not have specified a value, so you don't want to assume 'false' or 'true' - because they will have specific meanings. I guess I might need to re-evaluate my thinking a bit, but I like my user-interfaces to not force the user to enter all the fields - to the extent that nulls are used where the user has not specified an intentional value.

In my last post I was struggling to understand why Boolean in JavaFX can't be null, but it now makes more sense, knowing that Boolean is mapped to a primative boolean. Why does that make more sense?... Well, lets put our Java hat back on for a second...

public class JavaCode {

public boolean getPrimativeBoolean() {
return null; // WILL NOT COMPILE

public Boolean getObjectBoolean() {
return null; // THIS IS OKAY

So there we go, the answer is Boolean in JavaFX is a primitive boolean in Java, and because Java primitives can't hold a null, then you can't do it in JavaFX either. So I still need a solution to hold null values, but at least I understand why I can't using Boolean in JavaFX!

Of course I can simply wrap a primitive boolean in some simple class, but I am worried that I might cause some problems when I want to introduce binding later on.. i.e. I try to bind to my wrapper object, but the wrapper object doesn't actually change.

Oh, and that explains why String could be set to null, because in Java there is no primitive String - only the object based String. See, it all makes sense now ;-)

But, then they mention cardinality operators - take a look at this...

var variableName [: typeName] [? | + | *] [= initializer]; 
Operator Meaning
? Optional (For example, it can be null.)
+ One or more
* Zero or more

So, now it sounds like I can set my Booleans to be nullable?

var myBool : Boolean? = null; 
var myBool : String ? = null;

Hmm, well neither of those work for me! Netbeans says "Sorry, I was trying to understand a qualified identifier but I got confused when I saw '?'", bless - how honest! Maybe it's because I'm reading a PDF dated back to July 2007 and perhaps the language spec has changed since. Hopefully I will catch up with this a little more later.

Multi-line Strings
Unlike Java you can create a String over multiple lines...

var s = "This 
new lines";

Sequences (again)
In one of my posts I said that it would be nice to create a new sequence based on the 'values' of another sequence - rather than rely on the index numbers. Well, you can - and very nicely too...

"If some of the expressions inside the square brackets look odd, don't worry. They are actually Xquery-Update (similar
to XPath) predicates. In this case, the period inside the brackets does not refer to an index but instead to the value
of the index. JavaFX technology will test each one in the array until the expression is true, then apply the insert."

var x = [1,2,93,55]; 
insert 10 after x[. == 93]; // yields [1,2,93,10,55]

But that does not work for me in Netbeans "Sorry, I was trying to understand an expression but I got confused when I saw '.'." I am starting to get the feeling that either I don't have the latest version of Netbeans, or the plug-ins are out-of-date.

These examples do not work for me either:

// insert into weekdays after weekdays[. == 3];
// var a:Integer* = select n*n from n in [1..10];
// var a:Integer* = select n*n from n in [1..10] where (n%2 == 0);
// var a:Integer* = select n*m from n in [1..4], m in [100,200] where (n%2 == 0);

I did find some combinations that worked and that I was comfortable with

var days=["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];
var weekdays = days[0..5];
var daysStartingWithT = days[p | p.startsWith("T")];
var mixedUp = [ days[p | p.startsWith("S")], days [p | p.startsWith("T")] ];
for (day in mixedUp) {

Results in


Async - 'do'
Since my last blog covered asynchronous subjects, I found the following code block impressive - but alas, once more, I can't get it to run, it does not recognize 'do', I can't imagine that they have taken something so useful out. Perhaps someone can tell me what I am missing? :-(

// in the AWT Event Dispatch Thread (EDT) 
var result = new StringBuffer();
do {
// now in a background thread
var url = new URL("");
var is = url.openStream();
var reader = new BufferedReader(new InputStreamReader(is));
var line;
while (true) {
line = reader.readLine();
if (line == null) {
// now back in the EDT
System.out.println("result = {result}");

Does anyone know where I can get a PDF that contains all of the JavaFX language specification - I really need something up to date for the train ;-)

No comments:

Post a Comment