Over a million developers have joined DZone.

Building Rich Internet Applications Using Pivot and JavaScript

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

Pivot is a platform for building rich internet applications that run in the Java Plugin. While Pivot applications are often built using a combination of XML and Java, it is sometimes more convenient to build simple applications or applications whose code is likely to change often, such as prototypes, using a scripting language such as JavaScript. Pivot now allows developers to write their application logic using their scripting language of choice, using the features provided by the javax.script package available in Java 6 and above.

Pivot script code is stored in external files included by a WTKX source file, as shown below (WTKX is an XML markup language used by Pivot to simplify user interface construction):

<Border xmlns:wtkx="http://pivot-toolkit.org/wtkx/2008" xmlns="pivot.wtk">
    <content>
        <FlowPane orientation="vertical" styles="{padding:6}">
            <wtkx:script src="demo.js"/>

            <PushButton wtkx:id="pushButton" buttonData="Click Me!"
                buttonPressListener="$buttonPressListener"/>

            <Border styles="{padding:0, color:10}">
                <content>
                    <ListView listData="$listData" selectedIndex="0"/>
                </content>
            </Border>
        </FlowPane>
    </content>
</Border>

The <wtkx:script> tag is used to include the JavaScript code; all instance IDs declared in the WTKX file are available as variables to the script, and all variables declared in the script can be accessed using the WTKX dereference operator ("$"). The script in this example attaches a press listener to a push button and populates a list view component:

importPackage(Packages.pivot.collections);
importPackage(Packages.pivot.wtk);

var buttonPressListener = new ButtonPressListener() {
    buttonPressed: function(button) {
        Alert.alert("You clicked me!", button.getWindow());
    }
};

var listData = new ArrayList();
listData.add("One");
listData.add("Two");
listData.add("Three");

A screen shot of the result is as follows; a live example can be seen in the Demos section of the Pivot web site:

Pivot scripting support is not limited to JavaScript, however. Here is a similar example written using Groovy (I'm not a Groovy expert - thanks to Adam Stachelek at Cantina Consulting for contributing this example):

import pivot.collections.adapter.*
import pivot.wtk.*

public class MyButtonPressListener implements ButtonPressListener {
    public void buttonPressed(Button button) {
        Alert.alert("You clicked me!", button.getWindow())
    }
}

buttonPressListener = new MyButtonPressListener()

listData = []
listData << "One"
listData << "Two"
listData << "Three"

listData = new ListAdapter(listData)

The resulting application behavior is identical to the JavaScript version.

Pivot is a powerful, modern platform for creating rich internet applications, and now, thanks to the scripting support provided by Java 6, you don't have to be a Java developer to take advantage of it.

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}