Over a million developers have joined DZone.

Pivot: A Practical Example, Part 2 - Event Handling

DZone's Guide to

Pivot: A Practical Example, Part 2 - Event Handling

· Java Zone
Free Resource

Try Okta to add social login, MFA, and OpenID Connect support to your Java app in minutes. Create a free developer account today and never build auth again.

This is the second in a series of five articles that walk through the implementation of a simple but practical Pivot application called Stock Tracker. The previous article discussed building a user interface using WTKX, Pivot's XML markup language. This section focuses on event handling. The next article will cover "web queries", Pivot's native means of communicating with remote data services. -- Greg Brown

WTKX files do not include any code - all logic in a Pivot application is implemented in Java. Most of this logic is executed in response to an "event" triggered by some external source such as user input or the completion of an asynchronous operation running in a background thread.

In general, a Pivot application will load its user interface and wire up event handlers in the startup() method of the main application class. This method, along with the shutdown(), suspend(), and resume() methods, is defined by the Application interface, which all Pivot applications must implement.

Loading the UI

The Application interface in the Stock Tracker demo is implemented by the StockTracker class. The code in this file is referred to throughout the course of this section. A snippet of code from StockTracker's startup() method is shown below:

ApplicationContext applicationContext = ApplicationContext.getInstance();

// Set the locale
String language = applicationContext.getProperty(LANGUAGE_PROPERTY_NAME);
locale = (language == null) ? Locale.getDefault() : new Locale(language);

// Set the application context title
ResourceBundle resourceBundle =
ResourceBundle.getBundle(StockTracker.class.getName(), locale);


// Load the application's UI
ComponentLoader componentLoader = new ComponentLoader(locale);

Component content = componentLoader.load("pivot/tutorials/stocktracker/stocktracker.wtkx",

This code does the following:

  • Gets a reference to the application context, a singleton class that provides access to a number of system-level features and properties

  • Retrieves the "langauge" argument that was provided to the application context when it was created - for desktop applications, this is a command-line argument; in the browser, it is either passed as a query string argument or as an applet parameter

  • Creates a locale instance corresponding to the language argument

  • Obtains a resource bundle for the current locale

  • Sets the application context title (reflected in the frame title when running in a desktop application context)

  • Creates a new component loader using the current locale

  • Loads the Stock Tracker user interface from stocktracker.wtkx

This last step creates the component hierarchy defined in the WTKX files and performs any necessary resource substitutions. The result is a new component instance that can be set as the content of a window and shown to the user.

Adding Event Listeners

At this point, the entire UI has been created, but it is not yet visible. Even if it was, it wouldn't do much, since no event handlers have been added. The next thing the startup() method does is add a number of event handlers:

stocksTableView = (TableView)componentLoader.getComponent("stocksTableView");
stocksTableView.getTableViewSelectionListeners().add(new TableViewSelectionListener() {
public void selectionChanged(TableView tableView) {

addSymbolButton = (Button)componentLoader.getComponent("addSymbolButton");
addSymbolButton.getButtonPressListeners().add(new ButtonPressListener() {
public void buttonPressed(Button button) {

This code demonstrates how to obtain a reference to a component defined in a WTKX file and attach an event handler to it. Component references are retrieved from a loaded WTKX file via the getComponent() method of the ComponentLoader class. In this example, the components are defined in the top-level WTKX file, so no namespace prefix is necessary. Nested components can be retrieved using the fully qualified path (including namespaces) to the component. The sample code obtains references to two components: the table view that will contain the stock quotes and the "add symbol" button. Note that the return value of getComponent() must be cast to an appropriate type.

A caller signals its interest in a particular event by implementing an interface that defines an event handler method and adding itself as an event lister on the event source. In the example above, two event handlers are created: a selection change listener on the stock quote table view and a button press listener on the "add symbol" button. Some listener interfaces, such as those shown here, define only a single event handler method, but others define more than one and serve as a grouping of related events.

Note that, though these handlers are implemented as anonymous inner classes, this is not required - any class that implements the appropriate listener interface can register as a listener.

Displaying the Content

Finally, the application is ready to be shown. The component hierarchy loaded from the WTKX files is added to a Window and the window is opened, making the application visible and allowing the user to begin interacting with it:

window = new Window();
window.getAttributes().put(Display.MAXIMIZED_ATTRIBUTE, Boolean.TRUE);


ApplicationContext.setInterval(new Runnable() {
public void run() {


The application creates an instance of the undecorated Window class, sets the content component, and opens the window. The MAXIMIZED_ATTRIBUTE of the Display class is set to true so that, when opened, the window will be sized to fit the entire display area.

The code then calls refreshTable() to load the stock quote data and sets a timer interval to reload the data every 15 seconds. Finally, it sets the focus to the symbol text input, so a user can easily add a new stock to track.

Build and launch faster with Okta’s user management API. Register today for the free forever developer edition!


Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}