Pages

Subscribe:

Ads 468x60px

Saturday, July 24, 2010

Hello World part 1

So here we are going to do our first program in J2ME Hello World...

First We need to install an IDE to code, debug and run our mobile applications. There are several IDE's available NetBeans, Eclipse, Borland JBuilder.. etc. I personally go for the NetBeans IDE though it takes huge amount of your RAM :) . I'm using NetBeans 6.1 it's a bit older version. I found that there is a bug in newer versions that you can't run multiple emulators at the same time. I haven't tried the latest release NetBeans 6.9. So the first thing you have to do is that download NetBeans (I highly recommend 6.1 rather than the latest versions since you find it very difficult when you come to WMAPI, Bluetooth.. etc ) and install it in to your computer. (Before installing NetBeans hope you guys have installed Java Development KIT or we all known as JDK .. Click here)
Start your IDE and do the following..

1. Create a new Project.  File->New Project or Ctrl+Shift+N or Click Create New Project
    Select Mobility --> MIDP Application (My Project name is HelloWorld)
Create New Project
2. Give a name for your project. Make sure to uncheck Create HelloWorld  MIDlet
Create MIDP Application
3. Platform ,Device, Configuration , Profile Selection
Default Platform Selection
4. Create a package (My package name is hello)
Create a package
5. Create a new Midlet (My MIDlet name is Hello)

6. Now you have created a Midlet now lets start coding our first Mobile Application. This is a very simple program and when you press the "OK" button it pop up an Alert (I'll explain what is an Alert in Hello World  part 2 ). And when you press the "EXIT" button application will be closed. 
The code snippet is given below..

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Hello;

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

/**
 * @author Aruna Sujith
 */
public class Hello extends MIDlet implements CommandListener {

    Display myDisplay;
    Form myForm;
    Command ok;
    Command exit;

    public Hello() {
        myDisplay = Display.getDisplay(this);
        myForm = new Form("Hello");
        ok = new Command("OK", Command.OK, 1);
        exit = new Command("EXIT", Command.EXIT, 0);
        myForm.addCommand(ok);
        myForm.addCommand(exit);
        myForm.setCommandListener(this);

    }

    public void startApp() {
        myDisplay.setCurrent(myForm);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable arg1) {
        if (c == ok) {
            Alert alert = new Alert("Hello World!", "Hey It Really works!!!!", null, AlertType.INFO);
            alert.setTimeout(Alert.FOREVER);
            myDisplay.setCurrent(alert, myForm);

        } else if (c == exit) {
            destroyApp(true);
            notifyDestroyed();
        }
    }
}

To build the project; right click on the project --> click "Build" to build the project
To run the Peoject: right click on the project --> click "Run" to run the project or simply press F6 :-)
If you have completed the above instructions when you run the project you should have a emulator started.

The Emulator
Launch the program you will see two buttons press "OK" and "Exit" and see what happens. So this is it this is it. This is a simple program  and I' will explain more about the code and theory  in my next post " Hello World part 2 ". If you have any problem while doing this please put a comment. I'll answer as soon as possible to the best of my knowledge. Hope you'll enjoy programming mobile devices in J2ME .You can even try out this program in your own mobile. See you around... :-)  

2 comments:

  1. nice machan great work machan.....i want meet u and give some idea about j2me

    ReplyDelete