RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Java ME User Interfaces: Do It with LWUIT! : Page 5

Java ME user interfaces are about to get an exciting boost with the early release of a new library and tool from Sun called LWUIT.


Significant Features (cont'd)

Swing-like Event Handling
Events and rendering calls are all managed by the Display class in LWUIT. This class manages a single main thread (called the Event Dispatch Thread) that encapsulates the platform specific event delivery. LWUIT's event model is based on AWT 1.1's event architecture and generally operates as Swing/AWT does today. Thus, developers need only implement one of callback/listener interfaces in LWUIT's com.sun.lwuit.events package and register it with LWUIT in order to react to events (such as data changes, focus changes, actions, and selections) that occur in the components. Listing 4 shows code for an ActionListener and how to associate it to widgets. This listener, from the application shown above, reacts to students being selected, as well as the student "remove" and "add" button requests.

Figure 9. LWUIT's Layout Managers: From left to right: FlowLayout, BorderLayout, BoxLayout, and GridLayout.

Swing-like Layout Management
Layout management is also borrowed from Swing/AWT. In fact, layout management can help negotiate the differences in platform display sizes; along widgets to be displayed based on the dimensions of the screen rather than some fixed position. LWUIT offers a subset of the familiar Swing/AWT layout managers: FlowLayout, BorderLayout, BoxLayout, GridLayout, and GroupLayout. Swing/AWT does offer many more layout managers. However, when LWUIT's layout managers are used together with the Container for grouping components, these five offer significant control and flexibility over the device dictated layout management associated with LCDUI. For those that are unfamiliar with or have forgotten the basic layouts, examples of four of the five options are shown in Figure 9. From left to right are: FlowLayout (components are placed left to right in rows), BorderLayout (the screen is divided into directional north, east, south, west and center areas for components), BoxLayout (components are laid out in a single vertical or horizontal row—vertical is displayed here), and GridLayout (components are arranged in a square or rectangular grid—a 2x2 grid is shown in this example (GroupLayout offers more complex layout management that cannot be easily demonstrated).

Styles and Themes
Mobile devices, cellular phones in particular, are considered extremely personal items. So much so that a person's identity is usually somewhat reflected in their choice of device and how it is customized and configured. Most cell phones promote this customization by offering users the ability to pick a "theme" which controls the general look and feel of applications that run on the phone. There is even a Web site dedicated to offering themes (no lie—check out www.myfonethemes.com).

Figure 10. Widgets with Style: All LWUIT Components have an associated Style object. Here the Style object for a TextArea and Button are set with dramatically different Font and colors to highlight how Components Style objects can be used.

To support this demand, LWUIT defines a Style object that dictates the colors, fonts, transparency, margin, padding, and background image for each Component. Every LWUIT Component has an associated Style object. While setting and changing the Style object of each individual Component is possible (see Listing 5 and Figure 10 for an example), developing and using a theme is the preferred means to set the look and feel of an application.

Figure 11. The ResourceEdit Tool: LWUIT provides this tool to create and edit resource files. Resource files can contain images, fonts, themes, etc. that can be used by your Java ME applications to provide consistent look-and-feels.

A theme is a collection of color, font, transparency, margin, etc. properties. LWUIT's themes are similar in concept to cascading style sheets for web applications or Swing's Pluggable Look and Feel (PLAF) mechanism. A theme is stored in a resource file and is loaded and applied to all Components (or more appropriately to all Component Style objects) in the application. In the sample application, the intertechTheme.res file was loaded via LWUIT's Resources and UIManager to apply the Intertech, Inc.'s corporate colors, background logo, and look and feel to the demonstration application.

resources = Resources.open("/intertechTheme.res");
UIManager.getInstance().setThemeProps( resources.getTheme

The theme of an application, or Style object to any Component, can change without affecting the behavior of the application.

To help in the creation of the theme resource files, LWUIT ships with ResourceEdit (see Figure 11). This is a standalone tool that provides a preview mechanism helps you see the resulting look and feel on all of the components as the theme is being developed.


Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date