Leon Anavi
IT Tips && Tricks

Java ME

Created: 02.08.2009 17:12 Last Modified: 19.08.2009 14:43 Views: 4139
Keywords: Alert, AlertType, Display, Message

How to create a Message Dialog

Introduction to Java ME Alerts

JavaMe (Java Micro Edition) is a Java platform designed for mobile phones and embedded systems. To display a message to the user use class Alert. There are five predifined alert types at class AlertType:
  • INFO - shows information to the user
  • WARNING - warns the user of consequences after a certain operation
  • ERROR - alerts the user for an error during an activity.
  • ALARM - performs an event which the user has previosly requested.
  • CONFIRMATION - shows a message to confirm an action.
Message can be shown on the screen for a period of time of forever. To see what is the current period use method getTimeout. Calling the method setTimeout with appropriate argument will change the timeout. To make an alert visible until user response use Alert.FOREVER as an argument. Methods setTitle,setText or setType should be used to change the title, the text or the type of the message after its creation. Finally to make the message visible use instance of class Display and call method setCurrent.

Example

The example demonstrates how to create a method which shows an information message. The method should be called at appropriate location inside a class which extends MIDlet and implements CommandListener such as the method commandAction.
private void showMessageDialog(String sTitle, String sText)
{
	Alert MsgDlg = new Alert(sTitle, sText,
                          null, AlertType.INFO);
	//Hide Message only after an user request
	MsgDlg.setTimeout(Alert.FOREVER);
	//Make the message visible
	Display DisplayCtrl = Display.getDisplay(this);
	DisplayCtrl.setCurrent(MsgDlg);
}

Class Reference

Class Alert
Class Display
Class AlertType


  Home | About | Contact | Disclaimer | Sitemap 2009-2016 Leon Anavi. All rights reserved.