Pages

Saturday, February 27, 2010

Your first J2ME app using Java Platform Micro Edition SDK tool



Step 1:  Start Java Platform Micro Edition SDK tool by double clicking its icon or selecting it from the start menu.


Step 2:  From File menu click New-Project.


Step 3:  From "New Project" dialog, click "Java ME SDK" from Categories and click "MIDP Application" from Projects and click Next.


Step 4:  Type "HelloJ2ME" as Project Name, select the project location (leave defaults if not sure).


Step 5:  Click next to see following dialog. Dont change any values but do take a look at the options. We will discuss differences between CLDC and MIDP versions later.


Step 6: Click Finish. Now you see  your new work-place and an automatically generated initial code for you. And you'll see following code out there:





package hello;

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

public class HelloMIDlet extends MIDlet implements CommandListener {

    private Command exitCommand; // The exit command
    private Display display;     // The display for this MIDlet

    public HelloMIDlet() {
        display = Display.getDisplay(this);
        exitCommand = new Command("Exit", Command.EXIT, 0);
    }

    public void startApp() {
        TextBox t = new TextBox("Hello", "Hello, World!", 256, 0);

        t.addCommand(exitCommand);
        t.setCommandListener(this);

        display.setCurrent(t);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable s) {
        if (c == exitCommand) {
            destroyApp(false);
            notifyDestroyed();
        }
    }

}


Above code will be generated as a result of above wizard and we will later discuss the details of the code but before that let's run the application to see it in action. Simply ress "F6" to run the application. When you run an app, the IDE launches the J2ME Emulator and following is the output of the program.


J2ME programs, called MIDlets are the main software programs. If you have been programming for Java or similar languages,  you know that the program starts from the main() method but there is no main method in J2ME.

Execution of the program starts from startApp() method. Whenever a MIDlet program is started, its startApp() method is started automatically and the execution starts.

In this program, we have following code generated for you. Here's the explanation:

Create a TextBox control. The body of the textbox will show "Hello World", the title of the box will be "Hello", maximum number of characters that can be netered in this box is 256. Last argument of the consturcotr is the input constraings. The input is not constrained by any rules so the argument is 0. We will read about TextBoxes in more detail later.

TextBox t = new TextBox("Hello", "Hello, World!", 256, 0);


TextBox is a full screen data entry box that we use to display text only. If you want, you can type things in the box. But we do need to add an exit command to the menu of the TextBox so that you can exit the applciation.

        t.addCommand(exitCommand);
        t.setCommandListener(this);



This same class is the command listener, and has to implement the commandAction() method.

        t.setCommandListener(this);

 Finally display the box on the screen.


        display.setCurrent(t);


If this is your first time you are viewing this code and you have any confusions, please do not worry. As we proceed with the tutorials, things will start getting more clear for you.

 ------------------------------------

Related Links:

Start developing J2ME apps using Eclipse

How to setup J2ME development environment

How to make sure J2ME development environment is setup properly

No comments:

Post a Comment