Java Reference
In-Depth Information
The default behavior for Alert s automatically advances to the next screen when the Alert
is dismissed or times out. You can specify the next screen by passing it and the Alert to the two-
argument setCurrent() method in Display . If you call the regular one-argument setCurrent()
method, the previous screen is restored when the Alert is dismissed. Alert types serve as hints
to the underlying MIDP implementation. The implementation may use the alert type to decide
what kind of sound to play when the alert is shown. The AlertType class provides five types,
accessed as static member variables: ALARM , CONFIRMATION , ERROR , INFO , and WARNING .
There is an optional indicator to an Alert . By default, no indicator is present, but you can
add one by passing a Gauge to Alert 's setIndicator() method. ( Gauge is presented in the next
chapter in the section on Form s.) The indicator is handy for showing progress in a network
connection or a long computation.
The following example, TwoAlerts , shows both types of alert. It features a main TextBox
that is displayed when the MIDlet begins. Two commands, Go and About , provide access to the
alerts. The Go command shows a timed alert that contains a message about a fictitious network
error. The About command displays a modal alert that could contain copyright information. A
third command, Exit , provides a way to exit the MIDlet. Keep in mind that all three commands
may not fit on the screen; some of them may be accessible from a secondary menu.
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class TwoAlerts
extends MIDlet
implements CommandListener {
private Display mDisplay;
private TextBox mTextBox;
private Alert mTimedAlert;
private Alert mModalAlert;
private Command mAboutCommand, mGoCommand, mExitCommand;
public TwoAlerts() {
mAboutCommand = new Command("About", Command.SCREEN, 1);
mGoCommand = new Command("Go", Command.SCREEN, 1);
mExitCommand = new Command("Exit", Command.EXIT, 2);
mTextBox = new TextBox("TwoAlerts", "", 32, TextField.ANY);
mTextBox.addCommand(mAboutCommand);
mTextBox.addCommand(mGoCommand);
mTextBox.addCommand(mExitCommand);
mTextBox.setCommandListener(this);
mTimedAlert = new Alert("Network error",
"A network error occurred. Please try again.",
null,
AlertType.INFO);
Search WWH ::




Custom Search