A MIDlet Example to execute Alert Message with an Image

In this "Alert Message" example, you will learn how to send an alert message to user whenever an action is called.

A MIDlet Example to execute Alert Message with an Image

A MIDlet Example to execute Alert Message with an Image

     

In this "Alert Message" example, you will learn how to send an alert message to user whenever an action is called. The example have two different options 1. "ok" and option 2. "Cancel". If user choose the option one "ok" showMsg() method will be called but in case of choosing option two "Cancel" the tryAgain() method will be called.

An alert is a screen that shows message to the user and waits for a given period of time before proceeding to the next Displayable. An alert can contain a text string or an image.

Alert(java.lang.String title):-  This is used to build  new, empty Alert objects with the given title.

Alert(java.lang.Stringtitle, java.lang.StringalertText, javax.microedition.lcdui.ImagealertImage, javax.microedition.lcdui.AlertTypealertType):- we are using this to construct a new Alert object with the given title, content title, string and image, and alert type.

Alert(java.lang.String title, java.lang.String alertText, javax.microedition.lcdui.Image alertImage, javax.microedition.lcdui.AlertType alertType, Style style):- This Constructs a new Alert object with the given title, content string and image, and alert type.

Alert(java.lang.String title, Style style):- Used to create a new, empty Alert object with the given title.

The Alert class have following methods:

  • addCommand(javax.microedition.lcdui.Command cmd)
  • animate(long currentTime, ClippingRegion repaintRegion)
  • commandAction(javax.microedition.lcdui.Command cmd, javax.microedition.lcdui.Displayable thisScreen)
  • createCssSelector() 
  • getDefaultTimeout() 
  • getImage() 
  • getIndicator() 
  • getString() 
  • getTimeout() 
  • getType() 
  • removeCommand(javax.microedition.lcdui.Command cmd)
  • setCommandListener(javax.microedition.lcdui.CommandListener listener)
  • setCurrent(javax.microedition.lcdui.Display display, Alert alert, javax.microedition.lcdui.Displayable nextDisplayable)
  • setImage(javax.microedition.lcdui.Image img) 
  • setImage(javax.microedition.lcdui.Image img, Style style) 
  • setIndicator(Gauge indicator)
  • setIndicator(Gauge indicator, Style style) 
  • setString(java.lang.String str)
  • setString(java.lang.String str, Style style) 
  • setStyle(Style style) 
  • setTimeout(int time) 
  • setType(javax.microedition.lcdui.AlertType type) 
  • showNotify() 

 

 

 

 

Source Code of AlertExample .java

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;

public class AlertExample extends MIDlet implements CommandListener{
  private Display display;
  public Form form;
  private Command ok,cancel,done;
  private Image img, imge, img2;
  
  public AlertExample() {
  form = new Form("Roseindia Alert");
  cancel = new Command("Cancel", Command.CANCEL, 2);
  ok = new Command("OK", Command.OK, 2);
  try{
  img = Image.createImage("/logo.png");
  imge = Image.createImage("/front_left1_bad.png");
  img2 = Image.createImage("/Congratulations-1.png");
  }catch(Exception e){
  System.out.println(e.getMessage());
  }  
  }

 public void startApp() {
  display = Display.getDisplay(this);
  try{form.append(img);}catch(Exception e){}
  form.addCommand(cancel);
  form.addCommand(ok);
  form.setCommandListener(this);
  display.setCurrent(form);
  }

  public void pauseApp() {}

  public void destroyApp(boolean unconditional) {
  notifyDestroyed();
  }

  public void showMsg(){
  Alert success = new Alert("OK""Alert Message Here!"
  img2, AlertType.INFO);

  success.setTimeout(5000);
  success.setImage(img2);
  display.setCurrent(success, form);  
  }

  public void tryAgain() {
  Alert error = new Alert("Cancel""Alert Message Here!"
  imge, AlertType.INFO);

  error.setTimeout(5000);
  error.setImage(imge);
  display.setCurrent(error, form);
  }
  
  public void commandAction(Command c, Displayable d) {
  String label = c.getLabel();
  if(label.equals("Cancel")) {
  tryAgain();
  else if(label.equals("OK")) {
  showMsg();
  
  }
}

Download Source Code