J2ME tutorial – How to create a simple MIDlet application with NetBeans

The purpose of this tutorial is to present the basic elements needed for mobile Java applications (MIDlets) development. The notions described will be sufficient to build a simple application J2ME HelloWorld application.

Resources necessary for the project development are open-source and can be downloaded from the Internet:

  • visual development environment (IDE) that includes Java ME (Micro Edition), it is not mandatory, because sources can be compiled from the command line; the example described in this tutorial is created with NetBeans; this IDE, current version 6.9.1, can be downloaded at http://www.netbeans.org/ and although there are several distributions available select the one that contains Java ME technology; the least complex visual development environment used for Java mobile applications is included in Sun Java Java ME Software Development Kit (http://java.sun.com/javame/sdk/index.jsp), Sun Java Wireless Toolkit for CLDC;
  • emulators of mobile devices that support Java applications; such an emulator is already integrated in the distribution of NetBeans; the simulated mobile device is a generic one; if you want to test the application in a specific environment (for a specific mobile device) you may use resources made available to by the manufacturer; eg  for Nokia devices, a source for SDKs (Standard Development Kit) of different generations and models is forum.nokia.com;

The first thing to do is to create a Java ME mobile application (MIDlet).

  1. open NetBeans environment
  2. choose File -> New Project
  3. in Java ME category select a Mobile Application project type.
New J2ME Project in NetBeans

New J2ME Project in NetBeans

4.  define the location (and uncheck the Create Hello MIDlet)

5.  at the third step – Default Platform Selection, are defined next resources:

  • used emulator; implicitly, if you installed just NetBeans, without any SDKs, is available only Sun Java Wireless Toolkit 2.5.2 for CLDC;
  • type of mobile device (color or monochrome screen, qwerty keyboard);
  • configuration of the device, for this simple project select CLDC-1.0; CLDC or Connected Limited Device Configuration is a standard that describes the hardware features of your mobile device; version 1.0 of the standard describes a device with 128 KB for Java virtual machine, 32 MB dynamic memory, visual interface and limited ability to connect to a data network; this standard is defined by a consortium, which includes major manufacturers of mobile devices, to help software developers to establish the hardware limits of the device; also these settings have an impact on how the project will be compiled;
  • device profile; for this project select MIDP-2.0; MIDP or Mobile Information Device Profile is an extension of the hardware configuration (CLDC) describing the software characteristics of the device with reference to the available frameworks and Java virtual machine;
  • the chosen combination ( CLDC-1.0 MIDP-2.0) is valid for more than 75% of mobile devices released after 2005 (in your mobile device specifications you can find all of these).
Platform Selection for J2ME mobile application

Platform Selection for J2ME mobile application

Once the MobileApplication type project has been created we must define the context based on which we will build the application. This means inserting a MIDlet type resource using NetBeans menu options (File -> New File …) or through the context menu (New -> MIDlet) activated with right-click on the project name.
By adding a MIDlet resource type, a file with the .java extension, it is generated the MidletHelloWorld class, that is derived from MIDlet abstract class.

Initially, this class has the form:

import javax.microedition.midlet.*;
public class MidletHelloWorld extends MIDlet {
    public void startApp() {
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
}

and it overrides some abstract methods:

  • startApp() – starter function of the application (entry point); it is the first function to be executed, after the MidletHelloWorld instance has been created;
  • pauseApp() – the function is executed at the occurrence of an event that involves blocking the MIDlet application; a common scenario in a mobile device application includes receiving a phone call during the execution of the application;
  • destroyApp() – function used to close the application; it is executed at the end of the application and contains routines used to release resources; acts like a destructor function;

The code sequence, from the above sample, is the smallest and simplest MIDlet program. This program does not has any visual effects because it was finished immediately after it is launched in execution.

If we think to an application written in C, the MIDlet MidletHelloWorld, in current form, is equivalent to

void main ()
{
}

The three methods are required by the MidletHelloWorld class, because it is a a situation generated by derivation from a MIDlet abstract class. They manage possible states in which the application may be. This concept is also seen in other Java applications, like applets.

To post a message on the screen, the application must have access to the graphics resources controller’s. This is done by defining a Display object which is initialized at the start of the application.

public class MidletHelloWorld extends MIDlet {
    //reference to the application display manager
    private Display display = null;
    public void startApp() {
        if(display==null)
            display = Display.getDisplay(this);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
}

Because the hardware and software characteristics of mobile device impose restrictions on Java Mobile Applications (things are totally different from Java applications for PC), we can only develop applications that display information within a single form (or window) at a time.

Moreover, because the screen is small, this form covers all the device’s display. To manage the visual resources and to establish what form is active we will use the Display reference.

To display the on-screen text Hello World! we need a form and a container for that string. Thus we use a TextBox type form (this is NOT the TextBox control from other programming languages) which is a form containing only one dialog box control with a default Multiline property (text is displayed by default on multiple lines). The reason for the existence of this type of visual resource is given also by the limited resources we have on a mobile device. Other forms types allow more complex applications, but they will be described in another post.

By adding the TextBox form,the code becomes:

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class MidletHelloWorld extends MIDlet {
    //reference to the application display manager
    private Display display = null;

    //TextBox use to print the Hello Word message
    private TextBox tbMainForm;

    public MidletHelloWorld(){
      tbMainForm = new TextBox("My First MIDlet","Hello World !",100,0);
    }

    public void startApp() {
        if(display==null)
            display = Display.getDisplay(this);
	//activate the form
        display.setCurrent(tbMainForm);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
}

In the previous code sequence, you may notice that the form display was realized in the startApp method via the setCurrent function of the Display object.

Compiling and running the code we will obtain the next image in the emulator:

At the emulator start, it does not immediately execute the application. To start it you must select the Launch button (bottom right of the emulator).

Hello World Midlet example

Hello World Midlet example

Although the application is quite simple, it illustrates very well the structure of a MIDlet, and all these concepts are present in any other J2ME applications.

To test the mobile application in a real environment, your personal mobile phone, the MIDlet from this example must be installed on your mobile device. This is done by sending to the phone (by Bluetoth, IR, wireless or data cable) the two files that are in the dist subdirectory of the project (this is applicable only for Netbeans). If the project is called MobileApplicationHelloWorld, then the two files are MobileApplicationHelloWorld.jad (description of the application) and MobileApplicationHelloWorld.jar (application bytecode source ).