Replace Triggers are recursive

December 13, 2008

A replace trigger is a block of code attached to a variable that is executed when the variable’s value changes. Here’s the example from the JavaFX Tutorial:


var password = "foo" on replace oldValue {
     println("\nALERT! Password has changed!");
     println("Old Value: {oldValue}");
     println("New Value: {password}");
};

password = "bar";

When the script is run, it produces this output:


ALERT! Password has changed!
Old Value:
New Value: foo

ALERT! Password has changed!
Old Value: foo
New Value: bar

I wondered if triggers could be used to exclude certain values so I altered the code to experiment:


var password = "foo" on replace oldValue {
	if (password == "bar") {
		password = oldValue;
		println("'bar' is not allowed");
	} else {
		println("\nALERT! Password has changed!");
		println("Old Value: {oldValue}");
		println("New Value: {password}");
	}
};

password = 'bar';
password = 'password';

By running this version of the code, I confirmed a trigger can be used to filter input but I also discovered triggers are recursive:


ALERT! Password has changed!
Old Value:
New Value: foo

ALERT! Password has changed!
Old Value: bar
New Value: foo
'bar' is not allowed

ALERT! Password has changed!
Old Value: foo
New Value: password

When password = 'bar' is executed, the variable is changed to the new value and the trigger is fired. Since if (password == 'bar') evaluates to true, password is reset to oldValue. At this point, the trigger is fired again, the value is changed and the appropriate messages printed, then execution returns to print the message, “‘bar’ is not allowed.”
Nice to know info. This would definitely cause headaches if you didn’t know about it, especially during debugging.

JavaFX 1.0 requires Java 6u11?

December 12, 2008

I installed FX on my home machine last night and was surprised the install process prompted me to download Java SDK 6u11 (6u10 SDK was already installed, as well as the 6u11 JRE.) I didn’t remember if it was optional so I installed it at work today. Here’s the screen shot (a command line showing the installed Java version overlaid by the FX prerequisite dialog.)

screenhunter_01-dec-12-0800

It turns out I probably could have unchecked the download and continued but what’s odd is it reported it was looking for 1.6.0 or higher and found nothing. My setup at work was identical to my home machine (JDK 6u10 with the JRE updated to 6u11 by auto-update.)

Another oddity: from a command line, javafx -version reports the Java version, not the JavaFX version (see screen shot below.)

screenhunter_01-dec-12-1654

Begin at the beginning

December 11, 2008

I intend this blog to be a journal of my experiences as I learn JavaFX. It is primarily for my own purposes, though I’m happy if anyone else benefits.

There is a lot of hype surrounding JavaFX right now as well as some skeptics. My guess is its actual usefulness lies somewhere in between. I have two goals. Obviously, I want to learn the language enough to achieve competency and, if warranted, mastery. Secondly, I’ve been a back-end developer my entire eighteen-year career and I’m interested in exploring the user interface aspect of program development. I do not expect to achieve a high degree of expertise but I do hope to at some point be able to contribute, however minutely, to the artistic side.

To those ends, I downloaded and installed JavaFX SDK 1.0. Right away, I was confronted with a choice: do I download the SDK alone or with NetBeans? I use Eclipse exclusively but Sun pushes NetBeans hard. I decided to stick with Eclipse (for now) so I downloaded and installed the SDK. I was mildly surprised that Java 6u11 was a pre-requisite but as I was already on u10, no harm done. Does this mean 6u11 has to be installed on the target machine for the “Drag-to-Install” feature to properly work? Probably.

Since I decided to stick with Eclipse, I searched for a JavaFX plugin. The one from openjfx is apparently no longer available (Sun pushes NetBeans hard) so I found from Kenai that suffices so far. The only problem I had was that Eclipse locked the first time I tried to run a new script from within the IDE. I discovered I was okay if I create a new configuration before running each new script. I incorrectly assumed that a default configuration would be generated based on the highlighted script. The Kenai plugin does have several snippets that I will eventually explore. The Stage snippet generates the following code:

javafx.stage.Stage {
    title : "MyApp"
    scene: javafx.scene.Scene {
        width: 200
        height: 200
        content: [  ]
    }
}

So, there’s my first JavaFX script, ready to run.

Even though I’ve got the Eclipse plugin running, I’ll probably use it only as an editor. At least initially, I’ll compile and run from a command line. Sometimes I just like to do it the hard way.

ciao.


Follow

Get every new post delivered to your Inbox.