Java Reference
In-Depth Information
public void startApp() {
Display d = Display.getDisplay(this);
// ...
}
You may be tempted to call
getDisplay()
in a MIDlet's constructor, but according to the
specification,
getDisplay()
can only be called after the beginning of the MIDlet's
startApp()
method. If you try to call it before
startApp()
is called, your MIDlet may throw an exception
and terminate before starting!
Once you've got a reference to a device's
Display
, you'll just need to create something to
show (an instance of
Displayable
) and pass it to one of
Display
's
setCurrent()
methods:
public void setCurrent(Displayable next)
public void setCurrent(Alert alert, Displayable nextDisplayable)
The second version is used when you want to show a temporary message (an
Alert
) followed
by something else. We'll talk more about
Alert
s at the end of this chapter.
Display
's
getCurrent()
method returns a reference to what's currently being shown. Note
that a MIDlet may return a valid object from
getCurrent()
even if it is not visible to the user.
This could happen on a device running multiple MIDlets simultaneously, for example. Note
that the
Displayable
interface has a method called
isShown()
that indicates whether the given
object is actually being shown on the device screen.
You can also query a
Display
to determine its capabilities, which is helpful for applications
that need to adapt themselves to different types of displays. The
numColors()
method returns
the number of distinct colors supported by this device, while the
isColor()
method tells whether
the device supports color or grayscale. A
Display
for a device supporting 16 levels of gray, for
example, would return
false
from
isColor()
and
16
from
numColors()
. You can also find out
whether the device supports transparency by calling
numAlphaLevels()
, which returns the
number of transparency levels. The minimum return value is 2, indicating that image pixels
with full transparency and full opacity are supported. Return values greater than 2 indicate that
alpha blending is supported. The
getColor()
and
getBorderStyle()
methods are used for
finding out colors and line styles from the system user interface scheme. These are useful for
drawing custom items, a topic that is covered in Chapter 7. The
flashBacklight()
and
vibrate()
methods invoke the corresponding features of the device. These are more fully discussed in
Chapter 14.
Event Handling with Commands
Displayable
, the parent of all screen displays, supports a very flexible user interface concept,
the command. A
command
is something the user can invoke—you can think of it as a GUI
button. Like a button, it has a title, like OK or Cancel, and your application can respond appro-
priately when the user invokes the command. The premise is that you want a command to be
available to the user, but you don't really care how it is shown on the screen or exactly how the
user invokes it—keypad button, soft button, touch screen, voice recognition, whatever.
Every
Displayable
keeps a list of its
Command
s. You can add and remove
Command
s using the
following methods:
Search WWH ::
Custom Search