Creating your first MIDlet in Nokia IDE for Java ME

Download and install the latest Java™ SE Development Kit (JDK). The IDE is bundled with the latest Nokia Asha SDK 1.0.

When you launch the IDE for the first time, you must define the workspace directory.

The HelloWorld MIDlet consists of two classes: HelloWorldMIDlet, which is the main class and defines the basic functionality of the MIDlet, and HelloScreen, which defines the HelloScreen object used by the main class to display the text "HelloWorld!" on the device screen. In the Series 40 full touch version of the MIDlet, the HelloScreen class is also used to detect display orientation changes and adjust the MIDlet UI accordingly.

To create the HelloWorld MIDlet:

  1. Download and install Nokia IDE for Java ME (Eclipse).

  2. Select File > New > MIDlet Project.

    Tip: If you do not see MIDlet Project listed in the menu, select File > New > Other instead, select Java ME > MIDlet Project, and click Next.

  3. In the Project name field, enter HelloWorld.

    Figure: Creating a MIDlet project

  4. In the Configurations section, select the device platform for which you want to create the MIDlet.
  5. Click on the Device SDK Manager button.

    Figure: Selecting SDK for the MIDlet project

    Note: If you are uncertain as to which SDK you need to download, select your device from the Select Device drop-down list at the top of the window. This displays the relevant SDK to your device at the lower pane of the window. If the SDK for the platform of your choice is already installed on your local machine, select the check box next to it and select the Add button. Otherwise, select the Installed button next to the SDK you want to install.

  6. In the Install window, select the platform and then click Next.

    Figure: Installing SDK for the MIDlet project

  7. Click Finish and restart the IDE for changes to take effect. You can see the newly added platform under the Configurations section.

  8. Select the platform you have added in Configurations and click Next.

  9. In the MIDlet Name field, enter "HelloWorld".

  10. In the MIDlet Vendor field, enter your name.

  11. In the Microedition Configuration drop-down menu, select Connected Limited Device Configuration (1.1).

  12. In the Microedition Profile drop-down menu, select Mobile Information Device Profile (2.1).

  13. Click Finish. Nokia IDE sets up the MIDlet project.

    Figure: Configuring the MIDlet project

  14. If prompted, click Yes to open the Java ME perspective.

  15. To create the main class for the MIDlet, select File > New > Java ME MIDlet.

  16. In the Source folder field, make sure that the value reads "HelloWorld/src".

  17. In the Name field, enter "HelloWorldMIDlet".

  18. Click Finish. The HelloWorldMIDlet class is created in the default package.

    Figure: Creating the MIDlet main class

  19. Delete the contents of the HelloWorldMIDlet class and paste in the following source code:

    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.midlet.MIDlet;
    
    public class HelloWorldMIDlet
            extends MIDlet {
    
        public HelloWorldMIDlet() {
        }
    
        // Sets the MIDlet's current Display to a HelloScreen object.
        public void startApp() {
            Displayable current = Display.getDisplay(this).getCurrent();
            if (current == null) {
                HelloScreen helloScreen = new HelloScreen(this, "Hello, world!");
                Display.getDisplay(this).setCurrent(helloScreen);
            }
        }
    
        public void pauseApp() {
        }
    
        public void destroyApp(boolean unconditional) {
        }
    }
  20. To create the HelloScreen class, select File > New > Class.

  21. In the Source folder field, make sure that the value reads "HelloWorld/src".

  22. In the Name field, enter "HelloScreen".

  23. Click Finish. The HelloScreen class is created in the default package.

    Figure: Creating the HelloScreen class

  24. Delete the contents of the HelloScreen class and paste in the following source code:

    import javax.microedition.lcdui.*;
    
    class HelloScreen
            extends Form
            implements CommandListener {
    
        private final HelloWorldMIDlet midlet;
        private final Command exitCommand; // Exit command for closing the MIDlet in the device UI.
    
        public HelloScreen(HelloWorldMIDlet midlet, String string) {
            super("");
            StringItem helloText = new StringItem("", string);
            super.append(helloText);
            this.midlet = midlet;
            exitCommand = new Command("Exit", Command.EXIT, 1);
            addCommand(exitCommand);
            setCommandListener(this);
        }
    
        public void commandAction(Command command, Displayable displayable) {
            if (command == exitCommand) {
                midlet.notifyDestroyed();
            }
        }
    }

    Tip: To use the Series 40 full touch version of the HelloScreen class, see the source code.

  25. Save the project by selecting File > Save All.

  26. In the Package Explorer pane, right-click HelloWorld and select Export.

  27. Select Java ME > Export MIDlet Package and click Next.

  28. If the MIDlet configuration includes multiple device platforms, check the platform for which you want to package the MIDlet.

  29. In the Destination Directory section, check Use deployment directory. This instructs Nokia IDE to create the deployable MIDlet packages in the default deployment folder under the project folder.

    Note: You can only use a folder that already exists.

    Tip: If you want to use a different folder than the default folder, enter the path of the folder in the Directory field, or click Browse and select the folder.

    Note: You can only use a folder that already exists.

    Figure: Exporting the MIDlet to the default deployment folder

  30. Click Finish.

    Nokia IDE builds the MIDlet and creates the JAR and JAD files used for deploying the MIDlet to a device. If you have selected the default deployment folder, you can find the files in the Package Explorer pane under the corresponding folder.

    Figure: HelloWorld project viewed in the Package Explorer pane

Note: If you need to compile your code in an existing project in a different device Emulator or platform you can go back to the Device SDK Manager by clicking on the Package Explorer Application Descriptor folder.

The MIDlet has been created. You can now deploy it to a device.