Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions? | Software Development
 

Creating Midlet Application For Login in J2ME

This example show to create the Midlet application for user login.

Creating MIDlet Application For Login in 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 

In this example we will create a MIDlet (LoginExample), that will show following output display look like below: 

 

        

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

Give Your LoginID 'sandeep' and Password 'sandeep' 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.

 

 

 

Source Code Of LoginExample.java

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

 

Download Source Code

                         

» View all related tutorials
Related Tags: attributes diff size sed font attribute this show if for example exam ws sh e use im different m nt

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

4 comments so far (
post your own) View All Comments Latest 10 Comments:

i followed all the instuctions, it runs probperly but the problem is, I CANT SEE THE IMAGE! the logo, the front_left1_bad and the Congratulations-1. i put it all in the res folder. but still i cant see it in the app

Posted by gervel on Wednesday, 12.30.09 @ 12:28pm | #93690

thanks man its very useful. i got dome good idea here.

Regards

Posted by Seyed Reza Hasani on Saturday, 01.31.09 @ 01:25am | #84346

A super example...Thanks alot coz you have made my j2me learning period short.Will appreciate more tutorials on the same.Keep up the good work

Posted by Kimotho kahunja on Tuesday, 12.9.08 @ 09:18am | #82566

Creating Midlet Application For Login in 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
In this example we will create a Midlet (LoginExample), that will show following output display look like below:





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



Give Your LoginID 'sandeep' and Password 'sandeep' 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.













Source Code Of LoginExample.java

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



Posted by navin on Saturday, 10.11.08 @ 11:36am | #81009

 
Tell A Friend
Your Friend Name

 

 
Recently Viewed
Software Solutions
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.