Funding for 'IT Lab' Project, Phase 1: Progress of sticker sales. Purchase a sticker to help us reach our target.Updated: 2010-02-28 11:53
10.7%
Introduction to J2ME


by Manoj Alwis


Java Platform Micro Edition (Java ME), formerly and popularly known as Java 2 Micro Edition (J2ME) is a Java platform for embedded devices like mobile phones, PDA's, set-top boxes etc. J2ME customizes Java to these device capabilities. J2ME is optimized to smaller memory footprint and the core to this technology is KVM which is an optimized Virtual Machine for various devices. This optimization is based on memory, power consumption and connectivity of the respective devices.

  • A call handling feature of a mobile device is useless in set-top boxes.
  • Mobile device capabilities can vary from device to device, there are devices which support blue tooth and those which do not support Bluetooth.
  • A printing capability of a printer is useless in case of a set-top box.

J2ME takes care of all these scenarios by providing Configurations, Profiles and Optional Packages.


Figure shows basic J2ME architecture


Configurations provide core API's for a group of devices which are similar in nature, it provides base API's for Connection Framework, Memory issues etc. Currently J2ME has two configurations - Connected Limited Device Configuration (CLDC) and Connected Device Configuration (CDC).


Profiles are device specific API's and address issues like GUI, Persistent memory, device specific functionalities etc. Most popular profiles are Mobile Information Device Profile (MIDP), Personal Profile (PP) and Personal Basis Profile (PBP).


Relation between Profiles and Configurations

Profiles = Configurations + Basic Device specific features


Optional Packages are the additional features provided for Profiles. Let us think about the earlier mentioned scenario where there are mobile devices which support Bluetooth and those which do not support Bluetooth. J2ME has pushed Bluetooth to Optional Packages. This way Bluetooth Optional Package is supported only by the device which is Bluetooth enabled.


OEM Features: The device manufacturers can also provide additional API's which may be vendor specific like special User Interface API's, Printer API's etc.

J2ME platform has now got a wide industry support because of its flexibility in programming various devices and also because of the usual Java buzzword ``Write Once, Run Anywhere''.

Lesson(2)


Java ME CLDC/MIDP Development with NetBeans

This lesson guides you through the basic steps of using NetBeans IDE to create a Java™ Platform, Micro Edition (Java™ ME platform), Mobile Information Device Profile (MIDP) application and is designed to get you started with mobile application development as quickly as possible. The tutorial takes you through some of the basic steps of working with the project system. We show you two ways to create a Java ME MIDP project named "MyHello" that displays the text "Make my day" in a device emulator. This tutorial prepares you to use other IDE features for developing CLDC/MIDP applications.


Requirements


To complete this tutorial, you need the following software and resources:


For help getting your system set up, please see the installation instructions.



Software or Resource

NetBeans IDE with Java ME


Java Development Kit (JDK)

Version Required

Version 6.5 or
version 6.1

Version 6 or
version 5

Creating a First MIDP Application Using the Source Editor

Using the Source Code Editor, you manually create the code for your MIDlets. Creating code in the Source Code Editor gives you more flexibility when editing the code, and enables you to insert preprocessor code blocks. Next we create the MyHello application using the New Project and New File wizards, and complete the code using the Source Editor.


Creating a New Java ME MIDP Project

        1. Choose File > New Project. Under Categories, select Mobility. Under Projects, select MIDP Application and click Next.

        2. Enter MyHelloMIDlet in the Project Name field (note that "MID" is in upper case letters). Use the default Project Location, or change it to the directory you prefer on your system. We refer to this directory as $PROJECTHOME in this tutorial.

            3. Check the Set as Main Project checkbox and remove the check from the Create Hello MIDlet checkbox. Click Next.

       4. Select the Sun Java Wireless Toolkit 2.52 for CLDC as the Emulator Platform and use the remaining defaults. Click Next.

       5. Expand "Configuration templates provided by installed CLDC platforms" and "Sun Java Wireless Toolkit 2.52 for CLDC " folders. Check the boxes next to each of the configurations. The IDE automatically creates a new project configuration for each template listed.

       6.Click Finish. The IDE creates the $PROJECTHOME/MyHelloMIDlet project folder. The project folder contains all of your sources and project metadata, such as the project Ant script.

           7.Right-click the MyHelloMIDlet node in the Explorer window and choose New > MIDlet

           8.Enter HelloMIDlet as the MIDlet name (note that "MID" is in upper case letters by default). Click Finish. The HelloMIDlet.java file is created and the source code is displayed in the IDE's Editor window.


Figures shows how to create MIDP project



 


   

     9. Click in the Source Editor and change

1.    public class HelloMIDlet extends MIDlet

to

public class HelloMIDlet

extends MIDlet implements javax.microedition.lcdui.CommandListener

{


    10. Add the following text before the startApp() method:

 private void initialize() {

    javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_helloTextBox());

    }

   public void commandAction(javax.microedition.lcdui.Command command,    javax.microedition.lcdui.Displayable displayable) {   

    if (displayable == helloTextBox) {

    if (command == exitCommand) {

    javax.microedition.lcdui.Display.getDisplay(this).setCurrent(null);

    destroyApp(true);

    notifyDestroyed();

    }

    }

    }

    private javax.microedition.lcdui.TextBox get_helloTextBox() {

    if (helloTextBox == null) {

    helloTextBox = new javax.microedition.lcdui.TextBox(null, "Make My Day", 120, 0x0);

    helloTextBox.addCommand(get_exitCommand());

    helloTextBox.setCommandListener(this);

    }

    return helloTextBox;

    }

    private javax.microedition.lcdui.Command get_exitCommand() {

    if (exitCommand == null) {

    exitCommand = new javax.microedition.lcdui.Command("Exit", javax.microedition.lcdui.Command.EXIT,

    1);

    }

    return exitCommand;

    }

    javax.microedition.lcdui.TextBox helloTextBox;

    javax.microedition.lcdui.Command exitCommand;


   11. Add a line initialize(); to the startApp() method, so it looks like the following:

  public void startApp() {

initialize();

   }


   12.  Now let's add some text for our MIDlet to display.


    1. In the get_helloTextBox() method, replace the "test string" code with the text of your choice. For example, "Make my day."

    13.After all changes, full source code


import javax.microedition.midlet.*;

/**

 * @author User manoj alwis

 */

public class HelloMIDlet

extends MIDlet implements javax.microedition.lcdui.CommandListener  {

 javax.microedition.lcdui.TextBox helloTextBox;

 javax.microedition.lcdui.Command exitCommand;

 public void startApp() {

initialize();

   }

 private void initialize() {

    javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_helloTextBox());

    }

public void pauseApp() {

    }

   public void destroyApp(boolean unconditional) {

    }

public void commandAction(javax.microedition.lcdui.Command command,

 javax.microedition.lcdui.Displayable displayable) {

    if (displayable == helloTextBox) {

    if (command == exitCommand) {

    javax.microedition.lcdui.Display.getDisplay(this).setCurrent(null);

    destroyApp(true);

 notifyDestroyed();

    }

    }

    }

    private javax.microedition.lcdui.TextBox get_helloTextBox() {

    if (helloTextBox == null) {

    helloTextBox = new javax.microedition.lcdui.TextBox(null, "Make My Day", 120, 0x0);

    helloTextBox.addCommand(get_exitCommand());

    helloTextBox.setCommandListener(this);

    }

    return helloTextBox;

    }

    private javax.microedition.lcdui.Command get_exitCommand() {

    if (exitCommand == null) {

    exitCommand = new javax.microedition.lcdui.Command("Exit", javax.microedition.lcdui.Command.EXIT,

   1);

    }

    return exitCommand;

    }

  

}


Compiling and Running the Project

     1. Choose Run > Run Main Project (F6) from the Run menu. Follow the progress of the project compilation in the Output window. Note that the HelloMIDlet.java file is built before it is executed. A device emulator opens to display the results of the executed MIDlet. The default device emulator is DefaultColorPhone.

  2. In the device emulator window, click on the button below the Launch command. The device emulator launches the HelloMIDlet and displays the text you entered in the source code.

This is the end of the first j2me application. My next  tutorial  I will give you a new experience in j2me.

 

This is the end of the first j2me application. My next  tutorial  I will give you a new experience in j2me.

Share/Save
No votes yet

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options