Creating your first MIDlet in Eclipse

After setting up the development environment and configuring Eclipse, you can start creating the Hello World MIDlet. The 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 "Hello, world!" on the device screen.

To create the Hello World MIDlet:

  1. Select File > New > MIDlet Project.

  2. In the Project name field, enter "HelloWorld".

  3. In the Configurations section, select the device platform for which you want to create the MIDlet:

    • For Series 40 devices, select the Series 40 SDK.

    • For Symbian devices, select S60Emulator.

    If the platform you want to use is not listed:

    1. Next to the Configurations section, click Add.

    2. In the SDK drop-down menu, select the SDK you want to use.

    3. In the Device drop-down menu, select the device definition you want to use as the platform. For a Symbian device, select S60Emulator.

    4. Click Finish.

    Now that you have added the platform to the Configurations section, select it.

  4. Click Next.

    Figure: Creating a new MIDlet project

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

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

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

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

  9. Click Finish. Eclipse sets up the MIDlet project.

    Figure: Configuring the MIDlet project

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

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

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

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

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

    Figure: Creating the MIDlet main class

  15. Delete the content of the HelloWorldMIDlet class and paste in the following source code:

    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    
    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)
        {
        }
    
    }
  16. To create the HelloScreen class, select File > New > Class.

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

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

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

    Figure: Creating the HelloScreen class

  20. Delete the content 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();
            }
        }
    
    }
  21. Save the project by selecting File > Save All.

  22. In the Package Explorer pane, right-click HelloWorld and select Mobile Tools for Java > Create Package.

    Eclipse builds the MIDlet and creates the JAR and JAD files used for deploying the MIDlet to a device. You can find the files in the Package Explorer pane under deployed.

    If the MIDlet configurations include multiple platforms (step 3), Eclipse prompts you whether to package the MIDlet only for the selected ("active") platform or for all listed platforms. You can choose either option.

    Figure: HelloWorld project viewed in the Package Explorer pane

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