Creating your first MIDlet in NetBeans

After setting up the development environment and configuring NetBeans, 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 Project.

  2. Select Java ME > Mobile Application and click Next.

  3. In the Project Name field, enter "HelloWorld".

  4. Uncheck Create Hello MIDlet.

  5. Click Next. The MIDlet setup continues with device platform selection.

    Figure: Creating a new MIDlet project

  6. In the Emulator Platform drop-down menu, select the device platform for which you want to create the MIDlet:

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

    • For Symbian devices, select the Symbian SDK for Nokia devices you installed.

  7. If you selected a Symbian SDK, in the Device drop-down menu, select S60Emulator.

  8. Select CLDC-1.1 and MIDP-2.1.

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

    Figure: Choosing the device platform for the MIDlet

  10. To create the main class for the MIDlet, select File > New File.

  11. Select MIDP > MIDlet and click Next.

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

  13. In the MIDP Class 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 File.

  17. Select Java > Java Class and click Next.

  18. In the Class 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 Projects pane, right-click HelloWorld and select Deploy.

    NetBeans 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 Files pane under the dist folder.

    Figure: HelloWorld project viewed in the Projects pane

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