Java Reference
In-Depth Information
Figure 5-6.
Alerts are similar to modal dialog boxes in a desktop windowing system.
Alerts are represented by instances of the
javax.microedition.lcdui.Alert
class, which
offers the following constructors:
public Alert()
public Alert(String title, String alertText, Image alertImage, AlertType alertType)
Any or all of the parameters in the second constructor may be
null
. (Don't worry about the
Image
class right now; we'll discuss it in the next chapter in the section on
List
s.)
By default, timed
Alerts
are created using a default timeout value; you can find out the
default value by calling
getDefaultTimeout()
. To change the
Alert
's timeout, call
setTimeout()
with the timeout value in milliseconds. A special value,
FOREVER
, may be used to indicate that
the
Alert
is modal.
You could create a simple timed
Alert
with the following code:
Alert alert = new Alert("Sorry", "I'm sorry, Dave...", null, null);
To explicitly set the timeout value to five seconds, you could do this:
alert.setTimeout(5000);
If, instead, you wanted a modal alert, you would use the special value
FOREVER
:
alert.setTimeout(Alert.FOREVER);
The MIDP implementation will automatically supply a way to dismiss a modal alert. Sun's
reference implementation, for example, provides a
Done
command mapped to a soft button.
This command is exposed as the static member
DISMISS_COMMAND
, allowing you to register your
own command listener and explicitly recognize this command. You can add your own commands
to an
Alert
using the usual
addCommand()
method. The first time you call
addCommand()
, the
system's
Done
command is removed.
Search WWH ::
Custom Search