by May 9, 2001 0 comments

In this article, we’ll code a program for a Java-enabled embedded device. For developing Java applications for embedded devices, a J2ME toolkit is available, which comes along with the CLDC (Connected Limited Device Configuration), K virtual machine, and a profile called MIDP (Mobile Information Device Profile). This profile is targeted towards mobile phones and two-way pagers. The applications, which intend to run on these devices, can make use of the class libraries provided under this profile. Here, we’ll write code for a simple
MIDlet, a Java application that uses the MIDP. This MIDlet presents a text box, in which we have to enter a password. When we press an ‘OK’ button or key, a scrolling text called ticker tells you whether the password is correct or incorrect. We’ve provided the J2ME Wireless Toolkit for Windows on this month’s CD. Before installing it, you need to install JDK 1.3. You can find that also on the CD.

The code of our MIDlet called PasswordMIDlet is as follows:

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class PasswordMIDlet extends MIDlet implements CommandListener
private Command exitCommand, okCommand;
private Display display;
private TextBox textBox = null;
private Ticker ticker;
public PasswordMIDlet()
display = Display.getDisplay(this);
okCommand = new Command(“Ok”,Command.OK, 1);
exitCommand = new Command(“Exit”,Command.EXIT, 1);
textBox = new TextBox (“Enter Password :”,””,7,0×10000);
ticker = new Ticker (“Enter your access password : “);
public void startApp()
public void pauseApp()
public void destroyApp(boolean unconditional) 
public void commandAction(Command cmd, Displayable dis)
if(cmd == okCommand)
if(textBox.getString().compareTo(“PCQLABS”) == 0)
ticker.setString(“Correct Password”);
ticker.setString(“Incorrect Password”); 

if(cmd == exitCommand)

First, let’s run this code in the cellphone emulator provided with the J2ME Wireless Toolkit. If you’ve installed the Toolkit in the directory c:\j2mewtk, start the KToolbar application Start>Programs>J2ME Wireless Toolkit 1.0.1. Click on the button ‘New Project’. Enter PCQ for ‘Project Name’, PasswordMIDlet for the ‘MIDlet Class Name’, and click ‘OK’ when you’re presented with the ‘Setting for project PCQ’ dialog box. Type the above code in a text file and save it with the name in the directory
c:\j2mewtk\apps\PCQ\src. Now in the KToolbar, click on ‘Build’ and then on the ‘Run’ button. You’ll see the light brown colored cellphone emulator. Press ‘Enter’ to select the PCQ project, and you’ll see our PasswordMIDlet running inside the emulator. By clicking the keys of the cellphone, enter the password, which is
PCQLABS, and click on the white ‘OK’ key on the cellphone. If you entered the password correctly, the scrolling ticker that showed ‘Enter your access password:’ will now show ‘Correct Password’. In case of wrong password it will show ‘Incorrect Password’. 

In the PasswordMIDlet, we start by importing MIDP classes which belong to the packages javax.microedition.midlet and
javax.microedition.lcdui. Next we declare our class PasswordMIDlet, which extends or inherits the MIDlet class and implement the CommandListener interface. 

The MIDlet class is an abstract class–one that leaves the implementation of a method on the child class, PasswordMIDlet in our case. The MIDlet class provides the basic framework for a
MIDlet. It is similar to the Applet class for Java applets. In our
PasswordMIDlet, we override the methods startApp( ), pauseApp( ) and destroyApp( ) of the MIDlet class, which are called automatically when a MIDLet starts, pauses, and is destroyed or exited respectively. 

In our MIDlet, some events are generated when we press the ‘OK’ or ‘Exit’ button. To handle these events we implement the interface–which is similar to an abstract class–CommandListener by writing some code for the method
commandAction( ), which belongs to this interface. The method commandAction( ) is called whenever we press the ‘OK’ or ‘Exit’ button.

Now let’s focus at the constructor of our MIDlet, PasswordMIDlet( ). The constructor is called even before the
startApp( ) method. Hence we initialize all our GUI components like TestBox or Ticker in the constructor. The line

display = Display.getDisplay(this);

gives us a reference of the display environment in which the MIDlet will run and store it in a Display class object named display. The Display class contains methods to display GUI components on the screen of a cellphone or PDA. Next we create two commands or events as:

okCommand = new Command(“Ok”,Command.OK, 1);
exitCommand = new Command(“Exit”,Command.EXIT, 1);

These are mapped to any key depending on the device. In case of the cellphone emulator, the okCommand is mapped to the top-left key and the exitCommand is mapped to the top-right key. We pass three arguments or parameters for the creation of a command. The first one is a label, which will be displayed for the key associated with the command. In the next argument, for accepting some input the type will be Command.OK and for exiting the application, the type will be
Command.EXIT. Similarly there are other types like BACK, CANCEL, HELP, ITEM, STOP and SCREEN. There may be specific keys on the device, which will be associated to each command type. For example, pressing the red button on the cellphone emulator will call the exitCommand event. The last argument is a priority for each event. The lower the number, the higher the priority. 

Then we create a TextBox, where we enter the password, and a Ticker, which displays a scrolling text on the cellphone’s display. The arguments to the TextBox are its label, the initial text to be displayed in it, maximum characters it can accept, and the type of input it can accept. The number ‘0x10000’ indicates a password input and displays an asterisk (*) for each character input. Other types of inputs can be ANY (0), e-mail address (1), numeric (2), phone number (3) or URL (4). Only the password type has this hexadecimal constant (0x10000). The only argument for the Ticker is the text to be displayed. Through the lines


we add the Command objects to the display so that their label can be shown on the cellphone’s display. The TextBox class doesn’t have an
addCommand( ) method. This method belongs to an abstract class named Displayable, which contains methods to handle the GUI components on the cellphone or PDA screen. Since TextBox class inherits the Displayable class, the Command objects are passed to the superclass–Displayable. Next with


we specify that the events generated will be handled by the method
commandAction( ) present in the this class. The setCommandListener( ) method belongs to the superclass Displayable. In method
startApp( ) , we set the Ticker to the screen of the cellphone using the method
setTickler( ). This method belongs to the Screen class, a superclass of TextBox class (similar to Displayable). The Screen class has methods for showing titles and ticklers on the screen. Finally we show the textbox on the current cellphone display using the
setCurrent( ) method of the Display class. 

The commandAction( ) method is called whenever an event occurs. This method has two arguments: a Command object and a Displayable object. The Command object named cmd contains a reference to the command or event that has occurred. Hence, within this method, we use an if-else condition to check whether the event was generated by pressing the key corresponding to okCommand or
exitCommand. In the former case, we test whether the password typed in the textbox is
PCQLABS. If Yes, we set the ticker to show ‘Correct Password’, otherwise ‘Incorrect Password’. If the event is generated by the key labeled ‘Exit’, then we destroy the MIDlet and free resources used by its components by calling the
destroyApp( ) and notifyDestroyed( ) methods respectively.

Shekhar Govindarajan

No Comments so far

Jump into a conversation

No Comments Yet!

You can be the one to start a conversation.