The new applet experience

Applet development with JavaFX Script and Java SE 6u10

1 2 3 4 Page 2
Page 2 of 4
Learning Java FX Script
Jim Weaver's article "Creating Rich Internet Applications With Compiled JavaFX Script Technology" is an excellent resource for learning about JavaFX Script fundamentals. You should also check out Jim's Weblog "Helping you become a JavaFXpert." While perusing this Weblog, keep in mind that older entries focus on interpreted JavaFX Script, which presents a syntax that differs in various ways from compiled JavaFX Script syntax. For example, you won't find a var: self syntax (associate self with the current object) in compiled JavaFX Script.

Listing 1 is problematic because it obtains RSS documents on the event-dispatching thread, which can cause the UI to become momentarily unresponsive. Compiled JavaFX Script doesn't appear to offer a solution -- it doesn't even support interpreted JavaFX Script's do {} syntax for executing code on a background thread, which would have solved this problem.

I thought about using JavaFX Script's keyframe animation framework to help solve my "need for a background thread" problem. However, I quickly discovered that code executed by this framework also runs on the event-dispatching thread, not on a background thread as I require. For example, the following code outputs Thread[AWT-EventQueue-0,6,main] when run on my XP platform:

Timeline
{
   keyFrames:
   [
      KeyFrame
      {
         time: 1ms // The function() does not execute when 0 is assigned to this attribute.
         action: function()
         {
            // Would be nice to fetch RSS document here.
            System.out.println (Thread.currentThread ());
         }
      }
  ]
}.start ();

Unless I've missed something, I don't think that compiled JavaFX Script offers a SwingWorker-like Java solution for executing code on a background thread and updating the UI on the event-dispatching thread. However, I'm confident that this situation will be rectified as compiled JavaFX Script matures. Perhaps a solution will even emerge this week at JavaOne.

Creating and testing the applet

Before creating the Weather applet, make sure that you have a Java SE 6 JDK installed. Also, download and unarchive a recent build of the openjfx compiler -- I downloaded openjdk-compiler-weekly-2008-04-14.zip -- and add its bin directory to your PATH environment variable. Finally, unarchive this article's code archive and make sure that the directory containing Weather.fx is current.

Complete the following steps to create the Weather applet:

  1. Compile the source code: javafxc Weather.fx
  2. Create a JAR file for the Weather applet: jar cvf Weather.jar *.class bg.jpg

Assuming that you've successfully created Weather.jar, you're almost ready to test the Weather applet. First, however, you need to copy the compiled Java FX Script's javafxrt.jar and Scenario.jar files from their lib directory to the current directory. After doing this, invoke the following command to launch the Weather applet from appletviewer:

appletviewer -J-Djava.security.policy=my.policy Weather.html

Figure 1 reveals the Weather applet running in the context of appletviewer.

A screenshot of the Weather applet running in the context of appletviewer.
Figure 1. Type a zip code into the text field and press the Enter key to see the latest weather data associated with the zip code.

The applet requires permission to access Yahoo! Weather. It gets this permission from the my.policy file presented in Listing 2. (You'll also find my.policy in this article's code archive.)

Listing 2. my.policy

grant
{
   permission java.security.AllPermission;
};

The code archive also contains a Weather.html file whose <applet> tag I've modified to account for JavaFX Script. This file's HTML appears in Listing 3.

Listing 3. Weather.html

<html>
  <body>
    <applet code="javafx.ui.Applet" width=375 height=375
            archive="javafxrt.jar,Scenario.jar,Weather.jar">
      <param name="AppletClass" value="Weather">
    </applet>
  </body>
</html>

For JavaFX Script-based applets, the <applet> tag must specify javafx.ui.Applet as the value of the code attribute, and this tag must include an AppletClass parameter whose value is set to the name of the applet's main class (Weather). Also, the archive attribute must include the JavaFX Script runtime JAR files (javafxrt.jar and Scenario.jar) in addition to the applet's JAR file (Weather.jar).

1 2 3 4 Page 2
Page 2 of 4