O'Reilly Book Excerpts: Learning Wireless Java
MIDP GUI Programming, Part 1
This is the first in a series of articles from O'Reilly's Learning Wireless Java, by Qusay Mahmoud. This first section of Chapter 5 introduces us to MIDP GUI programming and considers whether to use the Abstract Window Toolkit (AWT).
User interface requirements for handheld devices are different from those for desktop computers. For example, the display size of handheld devices is smaller, and input devices do not always include pointing tools such as a mouse or pen input. For these reasons, you cannot follow the same user-interface programming guidelines for applications running on handheld devices that you can on desktop computers.
The CLDC itself does not define any GUI functionality. Instead, the official GUI classes for the J2ME are included in profiles such as the MIDP and are defined by the Java Community Process (JCP). You'll note that the GUI classes included in the MIDP are not based on the Abstract Window Toolkit (AWT). That seems like a major issue, which brings us to the following question.
Why Not Reuse the AWT?
After a great deal of consideration, the MIDP Expert Group decided not to subset the existing AWT and Project Swing classes for the following reasons:
- AWT is designed for desktop computers and optimized for these machines.
- AWT assumes certain user interaction models. The component set of the AWT is designed to work with a pointing device such as a mouse; however, many handheld devices, such as cell phones, have only a keypad for user input.
- AWT has a rich feature set, and includes support for functionality that is not found or is impractical to implement on handheld devices. For example, the AWT has extensive support for window management, such as resizing overlapping windows. However, the limited display size of handheld devices makes resizing a window impractical. Therefore, the window and layout managers within the AWT are not required for handheld devices.
- When a user interacts with an AWT-based application, event objects are created dynamically. These objects exist only until each associated event is processed by the application or system, at which time the object becomes eligible for garbage collection. The limited CPU and memory of handheld devices, however, cannot handle the burden.
The MIDP GUI APIs
Because of the issues outlined earlier, the MIDP contains its own abbreviated GUI, which is much different from AWT. The MIDP GUI consists of both high-level and low-level APIs, each with their own set of events. This chapter discusses and shows examples of using objects from both the high-level and low-level APIs. Handling events from APIs, however, is deferred to the next chapter.
The high-level API is designed for applications where portability between mobile information devices is important. To achieve portability, the API employs a high-level abstraction and gives you little control over its look and feel. For example, you cannot define the visual appearance (shape, color, or font) of the high-level components. Most interactions with the components are encapsulated by the implementation; the application will not be aware of them. Consequently, the underlying implementation does the necessary adaptation to the device's hardware and native user interface style. Classes that implement the high-level API all inherit the
The low-level API provides little abstraction. It is designed for applications that need precise placement and control of graphic elements, as well as access to low-level input events. This API gives the application full control over what is being drawn on the display. The
javax.microedition.lcdui.Graphics classes implement the low-level API. However, we should point out that MIDlets that access the low-level API are not guaranteed to be portable, because this API provides mechanisms to access details that are specific to a particular device.
The MIDP GUI Model
Here's how the MIDP GUI model works, in a nutshell. In order to show something on a MIDP device, you'll need to obtain the device's display, which is represented by the
javax.microedition.lcdui.Display class. The
Display class is the one and only display manager that is instantiated for each active MIDlet and provides methods to retrieve information about the device's display capabilities.
Obtaining the device's display is easy. However, this object by itself isn't very interesting. Instead, the more interesting abstraction is the screen, which encapsulates and organizes graphics objects and coordinates user input through the device. Screens are represented by the
javax.microedition.lcdui.Screen object and are shown by the
Display object by calling its
setCurrent( ) method. There can be several screens in an application, but only one screen at a time can be visible (or current) in a display, and the user can traverse only through the items on that screen. Figure 1 shows the one-to-many relationship between the display and its screens.
There are three types of screens in the MIDP GUI:
- Screens that entirely encapsulate a complex user interface component, such as a
Listcomponent is shown in Figure 2 and the
TextBoxcomponent is shown in Figure 3). The structure of these screens is predefined, and the application cannot add other components to these screens.
- Generic screens that use a
Formcomponent. The application can add text, images, and a simple set of related UI components to the form, which acts as a container.
- Screens used within the context of the low-level API, such as a subclass of the
Pages: 1, 2