JavaFX Script as a Web service client

JavaFX Script is Sun Microsystems' latest entry into the scripting language arena and it holds a lot of potential for Web services client development.

At the JavaOne conference in May, Sun Microsystems Inc. announced a new product line called JavaFX, composed of

JavaFX Script and JavaFX Mobile. The JavaFX scripting language is intended to make it easier to use the Java "swing" user interface library to create rich user interfaces that will run anywhere the Java standard edition is supported. Sun intends to release the language as an open source project. Note that people have taken to referring to JavaFX Script as just JavaFX or JFX, but Sun also include JavaFX Mobile under the general JavaFX line. I'm going to use JFX as short for JavaFX Script in this article.

Why another scripting language?

Scripting languages are certainly getting a lot of press these days. I have written about Ruby, JavaScript, Python, JavaServer Pages and Linden Scripting Language as Web service clients in previous articles. What these disparate languages have in common is an intent to make programming complex applications easier by providing simple ways to connect existing components. In the case of JFX, the components it is most concerned with are the Swing user interface components which first appeared in the Java standard library with version 1.2 to supplement the rather primitive Java AWT tools.

Basic JFX

The language supports the usual variable types for strings, integer and floating point numbers and Boolean values. Extensive support is provided for manipulating lists and arrays with syntax which looks quite powerful to me. The language also provides for creating and handling exceptions and adds the novel idea that any object can be thrown, not just the standard Java Throwable ones. JFX scripts can import Java classes, create Java objects and call their methods.

JFX objects are created with a declarative syntax. This is particularly useful in the classes which provide a simplified bridge to the "swing" Java user interface classes. Lets examine how JFX objects are created in a declarative style in the following complete script for the traditional first program.

import javafx.ui.*;

Frame {
    title: "Hello World from JavaFX"
    width: 200
    height: 80
    content: Button {
       text: "Hello World"
   }
   visible: true
}

In this example, Frame and Button are script classes which hide the details of creating the Java standard library JFrame and JButton objects that are then displayed on the screen. The Frame class has public "attributes" named "title", "width", "height ", "content" and "visible" which are set in the declaration.

The event processing problem

JFX shares this problem with every graphical user interface toolkit. User events such as clicking a button must not tie up the event processing thread with long running tasks because the interface will be unresponsive while the task is running. JFX provides the "do" and "do later" constructs to handle this problem. I experimented with a script to load data from a weather service site for display by JFX which illustrates the use of "do later." First I defined a JFX class named WeatherData. This class has two attributes and an operation.

import javafx.ui.*;
import java.net.URL;
import java.lang.StringBuffer ;
import java.lang.System;
import java.io.InputStreamReader;
import java.io.BufferedReader;

class WeatherData {
  attribute source: String ;
  attribute text: String ;
  operation update();
}

Note that JFX class declarations include only the signature of operations, the actual code is defined separately like this.

operation WeatherData.update(){
   var content = new StringBuffer("");
   do later {
      var url = new URL( source );
      var is = url.openStream();
      var reader = new BufferedReader(new InputStreamReader(is));
      var line;
      while (true) {
         line = reader.readLine();
         if (line == null) {  break;
         }
         content.append(line);
         content.append("\n");
      } // end while
      is.close();
      text = content.toString();
   } // end do later
}

Now to create the window to present the user interface. Note that the Frame is defined as having a local instance of the WeatherData class. The URL I used gets text giving predictions for my county for the next week.

Frame {
  var wdata = WeatherData {
     text: "initialized"
     source: "http://forecast.weather.gov/MapClick.php?zoneid=TXZ173&TextType=1"
  }
  title: "Weather Forcast"
  width: 400
  height: 400
  background: cyan
  content: BorderPanel {
     border: EmptyBorder{ top: 10 left: 10 bottom: 10 right: 10
     }
     top: Button {
        text: "update"
        action: operation(){ wdata.update();}
        height: 80
     }
     center: TextArea { text: bind wdata.text  
     }
   } // close content 
   visible: true
}

When the Button is clicked, the update operation is called, setting up a thread for the subsequent execution of the code in the "do later" block that fills in the text variable. Note that the text content of the TextArea has been defined with the "bind" keyword in terms of the wdata.text variable. When update has changed the content of the variable, the new text automatically appears in the window.

What is currently missing from JFX

The most obvious gap in the list of function the JFX language provides is a way of manipulating XML data with a syntax as convenient as that provided for manipulating swing objects. Of course you can create custom Java classes and use them in a JFX script, but it would be really convenient to be able to use XQuery or XPath syntax in dealing with XML-based Web services.

IDE support

I found plug-ins for both the NetBeans 5.5 and Eclipse IDEs. The NetBeans plug-in provided support for editing syntax and usage, but not complete JavaDocs style documentation. The NetBeans plug-in does not support graphical interface layout yet, but this should not be too hard to add. One commercial IDE builder, ReportMill, is experimenting with support for JFX in their report design editor.

What is the competition?

If JFX is going to be good for creating rich user interfaces by using Swing components, let's think about what that competes with. It appears to me that Flash is the most widespread rich user interface technology in use on the Internet today. Flash has the great advantage of being already installed in practically every Internet user's browser, easily updated as improvements are made.

Several years ago I looked at the Flex scripting language, which can be used to define Flash user interfaces in XML, but at that time licensing the developer's kit was rather expensive. Adobe's purchase of Macromedia has changed that situation, now there is a free Flex compiler and plans to release the "core" of Flex as an open source project while advanced tools continue as commercial products.

Conclusion

Sun's JavaFX Script is in the early stages of development. Although there is initial IDE support we still lack serious documentation. With proper support from Sun and the open source community, JFX will deserve consideration for the creation of sophisticated Web service clients.

Resources

The OpenJFX project home page, includes links for NetBeans and Eclipse plug-in downloads and tutorials.

JavaFX Script language description.

Home page for the Flex language, includes link for free SDK download.

ReportMill's experimental page layout editor.


 

This was first published in July 2007

Dig deeper on SOA, XML and Web Services Development Tools

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchSoftwareQuality

SearchCloudApplications

SearchAWS

TheServerSide

SearchWinDevelopment

Close