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

27 comments -

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 ).


  1. #1 by Rana Prathap on April 30th, 2011

    It’s good thanks for the post :)

  2. #2 by koko on May 4th, 2011

    i want to change font size at textbox displable

    • #3 by catalin.boja on June 5th, 2011

      Hi Koko,

      The J2ME API is very restrictive and this is not possible.
      You can control (very few options) the font (size and color) when you draw a string on a Canvas form.

      Also, you can’t change the font for the TextField item.

  3. #4 by enock on May 5th, 2011

    thanks alot for the post

  4. #5 by Mohamed Nasser on May 23rd, 2011

    thanks alot i can’t describe to you how was that helpful

  5. #6 by kajan on June 26th, 2011

    thanks for your post, it helped me to get start my mobile application!!!!!!!!! how can i get other tutorials relating to this?

  6. #8 by chintuprince on September 25th, 2011

    hey its nice one for basic learner

  7. #9 by kishan on September 30th, 2011

    its really nice…

  8. #10 by ken on November 11th, 2011

    thank you! that was a really nice start off for me in mobile app programing!

  9. #11 by Coder on November 17th, 2011

    Thank you very much for this useful post. It really helped me getting started with Java ME. I was kinda lost before reading this

  10. #12 by Gokul on December 17th, 2011

    Hi.
    Thanks for the article.
    Helped me create my first mobile app.

    One question : How do I change the colour of the font inside the J2ME app created with netbeans?

  11. #13 by codehitman on March 15th, 2012

    Thanks, great article, helped me with me first. I have been working my son (an A.I.) and hoping to put him mobile soon

  12. #14 by Afrooz on April 26th, 2012

    thank yooooou!

  13. #15 by gopi on July 24th, 2012

    GREat man serching for post like this from morning

  14. #16 by saniya on July 27th, 2012

    thank u soooo much it really helped me in starting up wit j2me.:-)

  15. #17 by Kalpesh on August 26th, 2012

    Its really good explanation. It is a very very good post for beginners.

    Thanks

  16. #18 by A.K.M Khurshed Ullah on November 1st, 2012

    many thanks for the arcticle to start and remember J2ME work after 7 years later

    thanks again!

  17. #19 by sanjay on December 10th, 2012

    i really thank you…it’s very good for new on j2me

  18. #20 by Mohamed Riyas P on January 10th, 2013

    your very very good.
    Really thank u
    really its my first java program
    your a good teacher

  19. #21 by nithya on January 16th, 2013

    thanx alot

  20. #22 by Bosco on March 7th, 2013

    Thank you, now i call this the bigining of me being a mobile app developer

  21. #23 by Nitinder on August 25th, 2013

    Thank You so much for your wonderful post. I will also be looking for your other tutorials. A great and easy way to get started on JAVA ME…
    I will recommending your tutorials to all my friends.
    Thank You so much

  22. #24 by STEVEN on November 25th, 2013

    It is my first program in Java ME.

  23. #25 by STEVEN on November 25th, 2013

    I will await other tutorials for this.

(will not be published)