Home J2me Creating Midlet Application For Login in J2ME
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Creating Midlet Application For Login in J2ME

Advertisement
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

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 3, 2008

Related Tutorials

Ask Questions?    Discuss: Creating Midlet Application For Login in J2ME   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
gayan
June 14, 2011
login

dear sir, i try to login the application by using this code on my application but it doesnt work,if need ill send my sample code regarding this. thankyou gayan
BA2011
July 29, 2011
Thank you

very much
Archana
August 16, 2011
Article is superb

The way u have explained is too good.I am a fresher for j2me so this is helping me a lot. TX
shailly
October 16, 2011
Image not gettin displayed

teh program is workin fine but the images are not gettin displayed.
dafny
October 21, 2011
connecting to database

how to retrieve contents from database using j2me
daniels
January 25, 2013
free

Trust in God
laksh
November 15, 2011
Creating MIDlet Application For Login in J2ME

its awesome its too good, can u pls let me how we can check the password in data and how we redirect from login page to application page. once its successful.
princypatel
December 28, 2011
j2me

in login example in which folder place image is place
Amit kumar gupta
January 30, 2012
java me

i like this site .i know about only java sdk but from this site we easily learn java me
shivangi
March 3, 2012
connect the page to the internet on j2me

its a great but i want to see the code to connect a one page to to internet link(like google.com). plz help me..........
mayank
May 7, 2012
launch button coding

sir ,when i am running this code it is not founding the midlet class and also the launch button is not working as there is no coding for launch button
Shashikant
June 6, 2012
Not Showing Images

Application is Running Fine but its not showing Images pls help Thanks In Advance :)
william
August 12, 2012
will

voy empezando a ver j2me y estos codigos la verdad son originales ayudan mucho al analisis y funcionan gracias
farhadfery
November 2, 2012
!

thanks
kuro
November 7, 2013
asa

you can use name and pass from account.txt ? EX account.txt : <name><pass>...
DMCA.com