Pivot: A Practical Example, Part 2 - Event Handling
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 =
// 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.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)