Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Using Alerts in Java ME

From Wiki
Jump to: navigation, search

This snippet shows how you can use the Alert object in Java ME.

Article Metadata
Code ExampleTested with
Devices(s): Nokia N78, Nokia N85, Nokia 5800 XpressMusic, Nokia 6131, Nokia C3-01, Nokia Asha 306, Nokia E7-00
Created: IlGolub (19 Feb 2009)
Reviewed: trashedDev (20 Aug 2012)
Last edited: hamishwillee (08 Aug 2013)



Alert is an object that can display some useful information on the screen, such as dialogues. You can choose between different types of Alerts. The Alert can be info, error, or confirmation, either getting the attention of the user for a few seconds, or requesting a confirmation from the user. An Alert object includes a title, message text, type, and optionally a Gauge or a custom image.

Source file:

* This Alert object represents a cofirmation alert.

private Alert confirmationAlert;
* This Alert object shows to us
* a info alert.

private Alert infoAlert;
* This Alert object uses for showing errors

private Alert errorAlert;
* Command for accepting action during coffee suggestion

private static final Command OK_ALERT_COMMAND =
new Command("Yes", Command.OK, 0);
* Command for renunciation of coffee suggestion

private static final Command CANCEL_ALERT_COMMAND =
new Command("No", Command.CANCEL, 0);
* Sets up the different kinds of alerts.

private void setupAlerts() {
//Sets up the confirmation alert.
//We set name of the picture, title, text, and type for alert
confirmationAlert = createSetupAlert("/confirmation.png",
"confirmation alert",
"Confirmation text", AlertType.CONFIRMATION);
//Adds comands to our Alert and sets up command listener
//Sets up the info alert.
//It's includes setting up title, text, and type for alert
//appropriately. This Alert has no picture.
infoAlert = createSetupAlert(null, "Info caption",
"Info message", AlertType.INFO);
//Initialization of error Alert. We set up only type.
//Others parameters will be created later
errorAlert = createSetupAlert(null, null,
null, AlertType.ERROR);
* Sets up the Alert objects
* @param imageName - name of image file, which sets as Alert image
* @param title - title for created Alert
* @param message - text body message for Alert
* @param alertType - type of Alert object (can be INFO, WARNING, ERROR,
* @return created Alert object

private Alert createSetupAlert(String imageName, String title,
String message, AlertType alertType) {
Image image = null;
//Creating Image if imageName presented
if(imageName != null) {
try {
// load the image for the image from file
image = Image.createImage(imageName);
} catch (IOException ex) {
//Creating Alert object with title, text message, image and type
Alert alert = new Alert(title, message, image, alertType);
//If alert type is CONFIRMATION
//then timeout for this is forever, and we must select command for
//switching to our mainDisplayableForm
if(alertType == AlertType.CONFIRMATION) {
} else {
//...else Alert will stay at the top for 4 seconds
return alert;
* From CommandListener.
* Called by the system to indicate that a command has been invoked on a
* particular displayable.
* @param command the command that was invoked
* @param displayable the displayable where the command was invoked

public void commandAction(Command command, Displayable displayable) {
if(displayable == mainDisplayableForm) {
if (command == EXIT_COMMAND) {
// Exit the MIDlet
} else if (command == CONFIRMATION_COMMAND) {
printString("Starting show Alert object...");
//show a coffee suggestion
printString("Alert object Ok!");
} else if (command == INFO_COMMAND) {
printString("Starting show Alert object...");
//shows an info alert
printString("Alert object Ok!");
} else if (command == ERROR_COMMAND) {
errorAlert.setTitle("Error title!");
errorAlert.setString("Error text!");
printString("Starting show Alert object...");
//shows an error alert
Display.getDisplay(this).setCurrent(errorAlert, mainDisplayableForm);
printString("Alert object Ok!");
} else {
if (command == OK_ALERT_COMMAND) {
//if we confirmed this message...
} else if (command == CANCEL_ALERT_COMMAND) {
//...otherwise printing that message


After starting this snippet, we can invoke three different commands for different types of alerts.

  • 'Confirmation alert' starts a dialogue with 'Confirmation text' message. The user can choose 'Yes' or 'No'.
  • 'Info alert' is an informational alert which displays an info message for a duration of 4 seconds.
  • 'Error alert' shows the error message for a duration of 4 seconds.

Supplementary material

This code snippet is part of the stub concept, which means that it has been patched on top of a template application in order to be more useful for developers. The version of the Java ME stub application used as a template in this snippet is v1.1.

  • The patched, executable application that can be used to test the features described in this snippet is available for download at
  • You can view all the changes that are required to implement the above-mentioned features. The changes are provided in unified diff and colour-coded diff (HTML) formats in
  • For general information on applying the patch, see Using Diffs.
  • For unpatched stub applications, see Example app stubs with logging framework.
This page was last modified on 8 August 2013, at 04:33.
127 page views in the last 30 days.