Oct 14, 2010

Create Login Form With J2ME

This example show to create the MIDlet application for user login . All MIDlet applications for the MIDP ( Mobile Information Device Profile) derived from  MIDlet class and it play a role as a mediator between the application and the environment in which the application runs. The MIDlet life cycle manage the flow of application. It is in the javax.microedition.midlet package, so import this package in your application. The javax.microedition.icdui package is used for following classes:


  • Alert
  • AlertType
  • Canvas
  • ChoiceGroup
  • Command
  • DateField
  • Display
  • Displayable
  • Font
  • Form
  • Gauge
  • Graphics
  • Image
  • ImageItem
  • Item
  • List
  • Screen
  • StringItem
  • TextBox
  • TextField
  • Ticker 


Give Your LoginID 'anak' and Password 'berbakti' then it call the commandAction where if condition will be executed and it calls a function (validateUser()) which checks whether name and password is equal to 'sandeep' or not if it equal to 'sandeep' then it executed the showMsg() function which show a congratulation message but if name and password is not equal to 'sandeep' then it call tryAgain() function which show the error page like figure below and it return on login page with refresh value.

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

public class LoginForm extends MIDlet implements CommandListener{
  private Display display;
  private TextField userName,password;
  public Form form;
  private Command login,cancel;
  private Image img, imge, img2;
      
  public LoginForm() {
    form = new Form("Sign in");
    userName = new TextField("LoginID:", "", 30, TextField.ANY);
    password = new TextField("Password:", "", 30, TextField.PASSWORD);
    cancel = new Command("Cancel", Command.CANCEL, 2);
    login = new Command("Login", Command.OK, 2);
    try{
      img = Image.createImage("/logo.png");
      imge = Image.createImage("/gagal.png");
      img2 = Image.createImage("/sukses.png");
    }catch(Exception e){
      System.out.println(e.getMessage());
    }    
  }

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

  public void pauseApp() {}

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

  public void validateUser(String name, String password) {
    if (name.equals("anak") && password.equals("berbakti")) {
      showMsg();
    } else {
      tryAgain();
    }
  }  

  public void showMsg() {
    Alert success = new Alert("Login Successfully", 
    "Login berhasil!", img2, AlertType.INFO);
    success.setImage(img2);
    userName.setString("");
    password.setString("");
    display.setCurrent(success, form);    
  }

  public void tryAgain() {
    Alert error = new Alert("Login salah", "coba lagi", imge, AlertType.ERROR);
    error.setTimeout(900);
    error.setImage(imge);
    userName.setString("");
    password.setString("");
    display.setCurrent(error, form);
  }
  
  public void commandAction(Command c, Displayable d) {
    String label = c.getLabel();
    if(label.equals("Cancel")) {
      destroyApp(true);
    } else if(label.equals("Login")) {
      validateUser(userName.getString(), password.getString());
    } 
  }
}
 Untuk Source Code Tutorial J2ME ini, silakan di download di:
http://rapidshare.com/files/424965756/LoginForm.rar

2 comments:

ahmad oriza sahputra said...

Thanks berat bro.. mantap banget keep posting...

ahmad oriza sahputra said...

Thanks berat bro... akhirnya nemuin pemecehan ditengah kebuntuan,,