Designs >> Creating a Demo MIDlet example - step by step guide Here is how one can create a sample demo MIDlet using J2ME Wireless
Toolkit 1.0.4.http://java.sun.com/products/j2mewtoolkit/download.html.
Basic imports for creating a Midlet are
Third party Advertisement
Packages
1. javax.microedition.io
2. javax.microedition.lcdui
3. javax.microedition.midlet
//package name as declared above
package com.demo;
//minimum imports for Midlets
//For input/output
import javax.microedition.io.*;
//For UI
import javax.microedition.lcdui.*;
//For Midlet to extends
import javax.microedition.midlet.MIDlet;
|
The demo midlet should extends javax.microedition.midlet.MIDlet
class and should implements javax.microedition.lcdui.CommandListener
interface.
//All Midlet should extends javax.microedition.midlet.Midlet class.
//and for handling the events should implements CommandListener
public class MyDemoMidlet extends MIDlet implements CommandListener
{
|
Let us declare private fields for the user interface and
commands for the menu options.
Here we shall try to create a login screen with a title,
a user name password entry fields and login, signup menu
options.
//Declare fields used for generating the UI
private TextField jtxt1;
private TextField jtxt2;
private Alert alert;
//Display is the look for user.
private Display display;
//Form contains the elements.
private Form frm;
//Create Command cmdLogin;
private Command cmdLogin;
//Command cmdSignup.
private Command cmdSignup;
|
The default constructor can be used to create command
objects or any other type of Business object.
//Default Constructor.
public MyDemoMidlet()
{
cmdLogin = new Command("Login",Command.OK,1);
cmdSignup = new Command("SignUp",Command.CANCEL,2);
}
|
The first method that is called is of course default
constructor is startApp(). It is having all the layout
code for the user interface and display and form setting.
As this Midlet class implements CommandListener interface.
All the events generated either by user or by system are
captured at the method commandAction(Command c, Displayable d).
Form object contains the Midlet as the listener object by
the code frm.setCommandListener((CommandListener)this);
//Life cycle methods of Midlet are startApp.
public void startApp()
{
display = Display.getDisplay(this);
//Creating the Object of the TextField.
jtxt1 = new TextField("Name :","",20,TextField.ANY);
jtxt2 = new TextField("Password :","",20,TextField.PASSWORD);
frm = new Form("Login Screen");
frm.append(jtxt1);
frm.append(jtxt2);
frm.addCommand(cmdLogin);
frm.addCommand(cmdSignup);
frm.setCommandListener((CommandListener)this);
//Set this form as the default screen.
display.setCurrent(frm);
}
|
The other two midlet lifecycle methods, such as pauseApp()
and destroyApp(boolean) are left blank as of now.
One can use these methods according to business logic or
resource maintenance, that is closing all the resources
used so far.
//pauseApp
public void pauseApp()
{
}
//destroyApp.
public void destroyApp(boolean status)
{
}
|
The commandAction(Command c, Displayable d) method
receives events.
//Method for the CommandListner interface
public void commandAction(Command c, Displayable d)
{
try{
if(c == cmdLogin){
//do something, such as connecting to web
server and validating the user.
}else if (c == cmdSignup){
//connect to web server and show the sign up screen.
}
}catch(Exception ex) {
}
}
|
After successfully building this midlet and running,
the same in WTK1.0.4, Follow to the same directory
structure as follows:
This tutorial is at its very initial stage and more
advanced tutorials will come in this site such as
connecting to web server from MIDlet and data
interchange etc.
Add your suggestions here