« October 2007 | Main | December 2007 »

November 2007

November 30, 2007

Playing with UI Features in Compiled JavaFX Script - and the Nimbus Look & Feel

As I've mentioned before, compiled JavaFX Script is currently under development, and one of the last pieces of the puzzle is the UI features.  Currently there are a few UI features that are operational in the compiled version, for example Frame, BorderPanel and Button.  Fortunately, the user interface API (widgets and 2D graphics) is almost identical to interpreted JavaFX Script, as is the declarative scripting.  For example, this classic border layout (BorderPanel widget) example executes in the interpreted, and compiled versions of JavaFX Script.  Here's the BorderPanelMini.fx code listing followed by a screenshot:

/*
*  BorderPanelMini.fx - A JavaFX program for use in a presentation
*
*  Developed 2007 by James L. Weaver (jim.weaver at lat-inc dot com)
*  to serve as a JavaFX Script example.
*/

import javafx.ui.*;
import java.lang.System;
import javax.swing.UIManager;
import sun.swing.plaf.nimbus.NimbusLookAndFeel;

UIManager.setLookAndFeel(new NimbusLookAndFeel());

Frame {
  title: "BorderPanel Mini-Example"
  height: 300
  width: 480
  visible: true
  content:
    BorderPanel {
      top:
        Button {
          text: "top"
        }
      center:
        Button {
          text: "center"
        }
      left:
        Button {
          text: "left"
        }
      right:
        Button {
          text: "right"
        }
      bottom:
        Button {
          text: "bottom"
        }
    }
}

Here a screenshot of the application:

Borderpanelmini


The Brand-New Nimbus Look & Feel

In the I Hear Voices post I mentioned the Consumer JRE project, which is now called Java SE 6 Update N.  According to this article by Chet Haase, this initiative has six main items that primarily address deployment and performance issues:

  • Quickstarter. Radically reduce the startup time for Java applications and applets.
  • Java Kernel. Reduce the time to install and launch when the user needs to install the JRE in order to run an application.
  • Deployment toolkit. Enable easy detection and installation of the JRE.
  • Installer improvements. Improve the user experience of installation.
  • Graphics performance on Microsoft Windows. Enable default graphics acceleration for simple and advanced 2D rendering.
  • Nimbus look and feel. Release a new cross-platform look and feel based on Synth.

I believe that this initiative will help enable the success of JavaFX.  The last item mentioned is the Nimbus Look and Feel, which I think is very attractive.  The screenshot above give a taste of the Nimbus Look and Feel, and the code example shows how to implement it in JavaFX Script programs.  You'll need to download the latest Java SE 6 Update N early access release in order to use the Nimbus Look and Feel.

Enjoy,
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications
eBook (PDF) download available at the book's Apress site


November 29, 2007

Puzzler4Compiled (JavaFX Puzzler 4) Results and Sample Solution

Congratulations to Shakir Gusaroff for submitting the first solution to the Puzzler4Compiled Puzzler, and the fact that it met the requirements of the problem!

For reference, here is my sample solution, followed by the console output:

*
*  Puzzler4Compiled.fx - A compiled JavaFX Script program that demonstrates
*                        features of compiled JavaFX Script
*
*  Developed 2007 by James L. Weaver (jim.weaver at lat-inc.com)
*/

import java.lang.System;

class Puzzler4Compiled {
   attribute changingText:String
     on replace {
       System.out.println("changingText was replaced with {changingText}");
     };
}

Puzzler4Compiled {
  changingText: "Hello"
}

Here's the console output:

changingText was replaced with Hello

Congratulations again, Shakir!  I'll contact you right away with instructions on downloading the JavaFX Script eBook.

Regards,
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications
Immediate eBook (PDF) download available at the book's Apress site

November 28, 2007

JavaFX Puzzler 4 - First Correct Solution Gets Free JavaFX Script eBook

As I mentioned in my post on the Learning JavaFX weblog a week ago, this JavaFX Puzzler will require writing a compiled JavaFX Script program.  In subsequent posts on that weblog, I've provided instructions for using the OpenJFX Compiler to compile a JavaFX Script program into Java bytecode, and to run the program.  If you haven't seen these posts, please review them.

The first person to post a comment with a correct solution will be given a free JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications eBook (PDF download).  When submitting your answer, be sure to put your correct email address in the  "Email Address: (Not displayed with comment.)" text field on the comment form so that I can contact you with the information to obtain your free eBook.

Requirements for Puzzler 4: The Puzzler4Compiled Program

Here is the console output of the sample solution:

changingText was replaced with Hello

The requirements for this program are as follows:

  1. The console output must be the same as shown above
  2. The program must define a class named Puzzler4Compiled that has an attribute named changingText that is of type String.
  3. The changingText attribute must have an on replace trigger that prints "changingText was replaced with" followed by the new value of the changingText attribute.
  4. An instance of the Puzzler4Compiled class must be created, setting the value of the changingText attribute to "Hello".  This causes the trigger to be executed, and the output shown above to be printed on the console.

Enjoy!
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications
Immediate eBook (PDF) download available at the book's Apress site

November 27, 2007

Compiled JavaFX if/else - Is it a Statement or an Expression?

Actually, it's both.  As I mentioned in the Express Yourself post,  compiled JavaFX Script has combined the functionality of for and foreach into just the foreach keyword.  I also explained in that post that compiled JavaFX Script has combined the if/else statement and ternary expression into just the if/else syntax.  Here's an example that you can compile and run to demonstrate this for yourself:

/*
*  CompiledIfElseExample.fx - A simple example in compiled JavaFX Script of
*  defining and iterating over a sequence.
*
*  Developed 2007 by James L. Weaver (jim.weaver at lat-inc.com)
*/

import java.lang.System;

class CompiledIfElseExample {
   function getAlias(name:String):String {
     var aka:String;
     if (name == "JAWA THE HUT" or name == "JAVA THE HUT") {
       aka = "The Garbage Collector";
     }
     else {
       aka =
         if (name == "JAH-VA") "The Hashcode Specialist"
         else "Captain of the Mustang"
     }
     return aka;
   }
}

var example =
  CompiledIfElseExample {
  };

System.out.println("JAWA THE HUT: {example.getAlias("JAWA THE HUT")}");
System.out.println("JAH-VA: {example.getAlias("JAH-VA")}");
System.out.println("DUKE: {example.getAlias("DUKE")}");


What's all this Jawa the Hut Stuff?

You caught me.  An ulterior motive for writing these blog posts nearly every day for the last month or so is to be prepared for speaking at the upcoming JavaPolis 2007 conference in Antwerp, Belgium.  I'll be doing two sessions on JavaFX: A three-hour University session on December 11, 2007, and a one-hour conference presentation on December 12, 2007.  The theme for the JavaPolis conference this year is Star Wars meets JavaPolis, so I crafted today's example to match the theme.  If you are in attendance, please introduce yourself!  By the way, these blog posts are not just to prepare for the JavaPolis conference -- some may appear in my next JavaFX book ;-)  Seriously though, I plan on posting nearly daily to this blog until JavaFX is the de facto standard for creating rich internet applications.

Back to the Example

This example shows the if/else construct as a statement beginning with the following line:

     if (name == "JAWA THE HUT" or name == "JAVA THE HUT") {

This form of if/else is identical to Java.  Like Java, the curly braces are optional.  What isn't like Java in this line is string equality.  Notice that it is legal in JavaFX to compare strings with the equality operator (==), but not in Java, where the equals() method is typically used for string equality.  Also different from Java is the or operator, which corresponds to the short-circuit or operator in Java (||).

This example also shows the if/else construct as an expression that returns a value in the following lines:

       aka =
         if (name == "JAH-VA") "The Hashcode Specialist"
         else "Captain of the Mustang"

In this case, the if/else construct replaces the ternary operator (?:) in Java, which evaluates to a value based upon the outcome of the if test.

Enjoy playing with this example, and as a reminder, the next JavaFX Puzzler will be be posted at 18:00 GMT (1:00 pm EST) on Wednesday, November 28, 2007.

Regards,
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications
Immediate eBook (PDF) download available at the book's Apress site







November 26, 2007

A Simple Compiled JavaFX Script Example of Using Sequences

To continue preparing you for the upcoming compiled JavaFX Script Puzzler, please compile and run the example in this post.  For instructions on obtaining the JavaFX Script Compiler, see the Obtaining the OpenJFX Script Compiler Just Got Easier post in this weblog.  As a reminder, the Puzzler will be be posted at 18:00 GMT (1:00 pm EST) on Wednesday, November 28, 2007. 

Here is the code for today's example, followed by an explanation of some compiled JavaFX Script concepts:

/*
*  CompiledSequenceExample.fx - A simple example in compiled JavaFX Script of
*  defining and iterating over a sequence.
*
*  Developed 2007 by James L. Weaver (jim.weaver at lat-inc.com)
*/

import java.lang.System;

class CompiledSequenceExample {
   attribute ordinals:String[] = ["zero", "one", "two", "three"];
   function printOrdinals():Void {
     System.out.println("Sequence elements:");
     foreach (element in ordinals) {
       System.out.println(element);
     }
   }
}

var example =
  CompiledSequenceExample {
  };

example.printOrdinals();


Compiled JavaFX Script Concepts in this Example

This example demonstrates how to define a sequence in compiled JavaFX Script.  In this case, the sequence is declared in the ordinals attribute of the CompiledSequenceExample class.  To indicate that a variable refers to a sequence, square brackets ([]) are used in compiled JavaFX instead of the asterisk (*) used in interpreted JavaFX.

As with the CompiledHelloJavaFX example in a previous post, this example initializes an attribute in the same statement in which is it is declared, in this case with a few names of ordinal numbers.

In interpreted JavaFX, there are two ways of defining a method: a function and an operation.  Compiled JavaFX simplifies this by combining both into a function.  If a function doesn't return a value, then it must be defined as returning Void in the manner shown in the example.

Speaking of simplification, as I mentioned in the Express Yourself post, the for and foreach functionality has been combined into foreach, which is used both in statements and expressions.  In today's example, foreach is being used as a statement to iterate and print the values of the sequence.

Enjoy!
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications
Immediate eBook (PDF) download available at the book's Apress site

November 24, 2007

Obtaining the OpenJFX Script Compiler Just Got Easier

My previous post walked you through compiling and running a simple compiled JavaFX Script program.  It required building the OpenJFX Compiler.  Today, I'm going to explain how to save some steps by downloading the latest build of the OpenJFX Compiler rather than building it on your machine.  If you decided not to build the compiler because it looked like a hassle, then relax -- this is a lot easier.  Just follow the instructions on the PlanetJFX page entitled How to Download the Latest Compiler Build Instead of Building It.

Note: The OpenJFX Compiler Project team plans on having a separate weekly build process soon, which will have versioned binary bundles.  This will make obtaining the compiler even faster and easier (and take up less space on your system). 

Compiling and Executing Your Program is a Little Easier Now Too

To compile the CompiledHelloJavaFX program from yesterday's blog post, navigate to its directory and use the following command:

javafxc CompiledHelloJavaFX.fx

To run the CompiledHelloJavaFX program, use this command:

javafx CompiledHelloJavaFX

Go and Do Likewise

Now that it's easier to obtain the OpenJFX Compiler, go ahead and play with compiling and running your own compiled JavaFX Script programs!  That way you'll be ready to give the JavaFX Puzzler a try.  As a reminder, it will be posted at 18:00 GMT (1:00 pm EST) on Wednesday, November 28, 2007.  Please post a comment if you have any questions.

Regards,
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications
Immediate eBook (PDF) download available at the book's Apress site

November 23, 2007

Developing Your First Compiled JavaFX Script Program - CompiledHelloJavaFX

As I said in this post a couple of days ago, the next JavaFX Puzzler will be posted at 18:00 GMT (1:00 pm EST) on Wednesday, November 28, 2007.  It will be a compiled JavaFX Script puzzler, so you'll need to build the JavaFX Script Compiler in order to participate in the Puzzler. 

Important Note: If you'd like to save some time and disk space by downloading the latest build of the JavaFX Script Compiler rather than building it on your machine, see the next post (Obtaining the OpenJFX Script Compiler Just Got Easier).

To help prepare you for the Puzzler, I'd like to you to develop a very basic "Hello World" style program.  After you've built the compiler, please copy and paste the following code into a file with the FX extension.  I chose CompiledHelloJavaFX.fx for my filename:

/*
*  CompiledHelloJavaFX.fx - A Hello World style compiled JavaFX Script program
*
*  Developed 2007 by James L. Weaver (jim.weaver at lat-inc.com)
*/

import java.lang.System;

class CompiledHelloJavaFX {
   attribute textToPrint:String = "Hello Compiled JavaFX Script!";
}

var chjfx =
  CompiledHelloJavaFX {
  };

System.out.println(chjfx.textToPrint);

This simple compiled JavaFX Script program is almost identical to what it would be if it were written in interpreted JavaFX Script.  The difference (attribute initialization) is noted in the Converting to the New Syntax document  that I've referred you to previously.

To compile the program, set your PATH environment variable to the dist/bin directory of where you installed the JavaFX compiler.  Then execute the proper javafxc command file for your operating system, passing your FX file as an argument.  On my Windows machine, I navigate to the directory in which my FX file is located, and enter the following from the command prompt:

javafxc CompiledHelloJavaFX.fx

To run your program, set the CLASSPATH environment variable to the javafxc.jar file in the dist directory as well as the current directory, passing in the name of the JavaFX Script program without the FX extension.  On my Windows machine, I enter the following from the command prompt:

java -cp C:\openjfx-compiler\dist\javafxc.jar;. CompiledHelloJavaFX

Note: If there is a package statement in your JavaFX Script program, then navigate to the directory in which the top node of the package is located.  See the Develop and Run Your First JavaFX Script Program in the Next Few Minutes post for a discussion on the package statement.

For a review, or to catch up if you're new to this Learn JavaFX blog, see the posts in the JavaFX Script category to learn the syntax of interpreted JavaFX Script programs.  As I've noted before, JavaFX Script is migrating from being an interpreted language to becoming a compiled language.  When compiled JavaFX Script is mature it will eventually replace interpreted JavaFX Script.  Please post any questions that you have as you build the JavaFX Script Compiler, and as you compile and run this example.

Enjoy,
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications
Immediate eBook (PDF) download available at the book's Apress site

November 22, 2007

Availability of JavaFX Script Book - PDF Download vs. Paperback

December 24, 2007 update to this post: The paperbacks of the JavaFX Script book are now shipping.

I have been asked about the availability of JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications.  The book is available as an immediate PDF download at http://www.apress.com/book/view/1590599454, and paperbacks should be shipping soon.  The book is based upon the interpreted (current) version of JavaFX.  The compiled version of JavaFX Script is still in flux, and when the first milestone release is available, I will quickly update the book.  The Apress "firstPress" brand is geared toward this purpose, in that the eBook can be made available quickly, and the paperback is typically "print on demand".  I have been involved in providing input to the JavaFX compiler development team, and am currently assisting in the testing efforts, so the process of updating the book for the compiled version should go very quickly.

By the way, the UI libraries won't change much in the first release of the compiled version -- only some of the syntax of the language.  Please see the Putting My CTO Hat On post for my company's plans for use of the current (interpreted) version, and the compiled version, of JavaFX Script.

Regards,
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications
Immediate eBook (PDF) download available at the book's Apress site

November 21, 2007

How to Build the Open JavaFX Script Compiler - Next JavaFX Puzzler Alert

Most of the posts in the Compiled JavaFX Script category of this blog will have code examples (for an example see, Achieving Closure in Compiled JavaFX Script).  Consequently, it will be very helpful for you to have the Open JavaFX Script Compiler so you can play with the examples.  Tom Ball (the Open JavaFX Script Compiler project leader) has provided some instructions on PlanetJFX for building the compiler.

By the way, the next JavaFX Puzzler is going to require writing a compiled JavaFX Script program.  This JavaFX Puzzler will be posted at 18:00 GMT (1:00 pm EST) on Wednesday, November 28, 2007.  That should give you plenty of time to download and build the compiler, as well as to learn from past and future posts in the Compiled JavaFX Script category . ;-)

Regards,
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications
eBook (PDF) download available at the book's Apress site

November 20, 2007

Achieving Closure in Compiled JavaFX Script

One of the very useful (and cool) features of compiled JavaFX Script will be closures.  In a nutshell, JavaFX Script closures provide the ability to define a function within another function with the inner function having access to the local variables of the outer function.  This feature is enabled by the fact that in compiled JavaFX Script, functions are first-class objects, which provides the ability to assign functions to variables and to pass functions as arguments to other functions.  You can read more about closures in this Wikipedia article.

Closures are often used to implement callback functions, many times handling UI events.  Since the UI part of compiled JavaFX Script isn't ready for prime-time yet, I'd like to show you a callback example in which a function is called when a timer expires.  You can run this example by downloading and building the JavaFX Compiler in its current state from the Subversion repository in the OpenJFX Compiler Project.  Here's the source code for the example:

/*
*  ClosureExample.fx - Demonstrates closures, and that functions are first-class
*                      objects in compiled JavaFX Script.
*
*  Uses JavaFX Script compiled version
*  Developed 2007 by James L. Weaver (jim.weaver at lat-inc dot com)
*/

import java.lang.System;
import java.lang.Thread;

public class ClosureExample {

  function startTimer(millis:Integer, wakeUp:function():Void):Void {
    Thread.currentThread().sleep(millis);
    wakeUp();
  }
 
  function setAlarm(sleepTime:Integer):Void {
    var message = "Time to wake up!";
    var wakeUpFunc =
      function() {
        System.out.println(message);
        System.out.println("You slept for {sleepTime} milliseconds");
      };
    startTimer(sleepTime, wakeUpFunc);
   
  }
}

var example =
  ClosureExample {
  };
 
example.setAlarm(3000);

Walking Through the Closure Related Features of this Example

After creating an instance of the ClosureExample class, and calling its setAlarm() function, you'll notice that an anonymous function is defined and assigned to a variable named wakeUpFunc.  After that, the startTimer() function is invoked, passing the desired duration of the timer, as well as the wakeUpFunc reference.

Within the startTimer() function, the sleep() method of the Java Thread class is called, which causes the current thread to sleep for the desired number of milliseconds.  This, by the way, demonstrates JavaFX Script's ability to leverage the power of Java classes (see the Putting My CTO Hat On post).  After the thread wakes up, the callback function that was passed into the startTimer() function is invoked, which performs the closure.

The net effect is that the callback function accesses and prints the values of local variables of the setAlarm() function.  The closure feature allows access to these variables from within the scope of the callback function, even though they're not local to the callback function.

Regards,
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications
eBook (PDF) download available at the book's Apress site