Funding for 'IT Lab' Project, Phase 1: Progress of sticker sales. Purchase a sticker to help us reach our target.Updated: 2010-02-28 11:53
10.7%
Creating MIDlet Application For Login in J2ME

  by Manoj Alwis

 

Hi friends,

This Lesson(4) example I will show you how to create the MIDlet application for user login . 

First of all you have to  create a MIDlet (LoginExample), that will show following output display look like below: 

 

 

Click on 'Launch' Button then the login page display like below:

 

 

 

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

 

 

If an Invalid login id or password is entered:-

 

 

 

Source Code Of LoginExample.java

 

 

 

//by manoj alwis

import javax.microedition.midlet.MIDlet;

import javax.microedition.lcdui.*;

 

public class LoginExample extends MIDlet implements CommandListener{

  private Display display;

  private TextField userName,password;

  public Form form;

  private Command login,cancel;

  private Image img, imge, img2;

 

  public LoginExample() {

    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{

   

    }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("manoj") && password.equals("123456")) {

      showMsg();

    } else {

      tryAgain();

    }

  }

 

  public void showMsg() {

    Alert success = new Alert("Login Successfully",

    "Your Login Process is completed!",

     img2, AlertType.INFO);

    success.setImage(img2);

    userName.setString("");

    password.setString("");

    display.setCurrent(success, form);

  }

 

  public void tryAgain() {

    Alert error = new Alert("Login Incorrect", "Please try again", 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());

    }

  }

}

 

That’s all in Lesson(4), See you next month………………..

 

Share/Save
No votes yet

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options