Over a million developers have joined DZone.

Building Rich Internet Applications Using Pivot and JavaScript

DZone's Guide to

Building Rich Internet Applications Using Pivot and JavaScript

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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">
        <FlowPane orientation="vertical" styles="{padding:6}">
            <wtkx:script src="demo.js"/>

            <PushButton wtkx:id="pushButton" buttonData="Click Me!"

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

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:


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

var listData = new ArrayList();

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.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat


Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}