Creating your first MIDlet in NetBeans

Download and install the latest Java™ SE Development Kit (JDK). The JDK is required for compiling Java classes. The NetBeans installation Guide asks you to browse to the JDK location on the local drive during installation.

Note: When installing NetBeans, select to customize the installation and clear the Features on Demand option. Download and install a software development kit (SDK) that supports Java ME. The SDK provides Java ME class libraries that the IDE requires for building MIDlets for a particular device platform.

To create MIDlets for Series 40 devices, use a Nokia SDK for Java. If you are creating MIDlets for Series 40, 6th edition, or earlier Series 40 devices, use the corresponding Series 40 SDK.

Note: To ensure that the SDK is properly integrated with the IDE, install the SDK on the same logical drive as the IDE.

To configure NetBeans:

After installing the required software, integrate NetBeans with the installed SDK:

  1. Open NetBeans.

  2. Select Tools > Java Platforms.

  3. Click Add Platform.

  4. Select Java ME CLDC Platform Emulator and click Next. NetBeans searches your computer for SDKs that support Java ME.

    Figure: Selecting the Java platform type

  5. If NetBeans does not find the SDK, click Find More Java ME Platform Folders and select the folder where you installed the SDK. NetBeans searches the selected folder for SDKs that support Java ME.

  6. Select the SDK and click Next. NetBeans detects the SDK capabilities.

    Figure: Adding a Series 40 SDK

  7. To complete the configuration, click Finish and then Close.

Your development environment is now set and you can create the MIDlet in NetBeans.

To create the HelloWorld MIDlet:

  1. Download and install NetBeans (select an installation bundle that supports Java ME).

  2. Select File > New Project.

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

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

    Figure: Creating your first MIDlet in NetBeans

  5. Clear the checkbox Create Default Package and Main Executable Class.

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

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

    • For Series 40 devices, select a Nokia SDK for Java.

    Figure: Choosing the device platform for the MIDlet

  8. Select CLDC-1.1 and MIDP-2.1.

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

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

  11. Select CLDC > 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 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) {
        }
    }
  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 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.

  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 is created. You can now deploy it to a device.