Java ME MIDP Development Quick Start Guide

This tutorial guides you through the basic steps of using NetBeans IDE to create a Java™ Platform, Micro Edition (Java™ ME platform), Mobile Information Device Profile (MIDP) application and is designed to get you started with mobile application development as quickly as possible. The tutorial takes you through some of the basic steps of working with the project system. We show you two ways to create a Java ME MIDP project named "MyHello" that displays the text "Make My Day" in a device emulator. This tutorial prepares you to use other IDE features for developing CLDC/MIDP applications.

Note: If you are using NetBeans IDE 7.2 or 7.2.1, refer to the NetBeans IDE 7.2, or 7.2.1 Java ME MIDP Development Quick Start Guide.

Contents

Content on this page applies to NetBeans IDE 7.3

Requirements

To complete this tutorial, you need the following software and resources:

Software or Resource Version Required
NetBeans IDE with Java ME Version 7.3
Java Development Kit (JDK) Version 6 or 7
Oracle Java ME SDK Depends on the operating system

Important: The distributions of NetBeans IDE 7.3 bundle neither Sun Java Wireless Toolkit nor Oracle Java ME SDK (both of them must be downloaded and installed separately).

Important: The required version of Oracle Java ME SDK varies depending on your operating system:

Setting Up the IDE with Oracle Java ME SDK (Windows only)

Important: This section applies to Windows platforms only.

To set up the NetBeans IDE with the Oracle Java ME SDK for the Windows platform, you need to:

  • install Oracle Java ME SDK
  • activate the Java ME feature in the NetBeans IDE
  • install the Oracle Java ME Platform SDK in the NetBeans IDE

To install Oracle Java ME SDK:

  1. Go to the Java ME SDK download page.
  2. Click the Accept License Agreement option.
  3. Download the oracle_java_me_sdk-3_2.exe installer file and save it to any location on your computer.
  4. Double-click the installer file to run the installer, and follow the instructions.
    When the installation is complete, the Java (TM) ME Platform SDK 3.2 Device Manager icon ( Java (TM) ME Platform SDK 3.2 Device Manager icon) appears in the Windows system tray.

To activate the Java ME feature:

  1. Choose Tools > Plugins from the main IDE's toolbar.
  2. In the Plugins dialog box, click the Installed tab and select Java ME in the list of available features.

    Plugins dialog box

  3. Click Activate.
  4. At the Welcome panel of the Installer dialog box, click Activate.

    Installer dialog box

  5. When the activation is successfully completed, click Finish.
  6. Click Close to close the Plugins dialog box.

To install the Oracle Java ME Platform:

  1. Choose Tools > Java Platforms.
    The Java Platform Manager dialog box displays.

    Name and Location panel

  2. Click Add Platform.
  3. In the Select Platform Type of the Add Java Platform wizard, select Java ME CLDC Platform Emulator and click Next.
    The IDE displays the Choose directory to search for platforms dialog box.
  4. Select the directory you installed the Java ME SDK Platform to (for example, C:\Java_ME_Platform_SDK_3.2) and click Open.
  5. In the Platform Folders panel of the Add Java Platform wizard, ensure the platform you selected in the previous step is chosen and click Next.
  6. As soon as the IDE detects the platform, click Finish in the Detected Platforms panel of the the Add Java Platform wizard.

    J2ME Platform detected

    The Oracle Java ME Platform is now registered in the Netbeans IDE.

    J2ME Platform registered

  7. In the Java Platform Manager dialog box, click Close.

Now your NetBeans IDE is set up for mobile development.

Creating a MIDP Application Using the Source Editor

Using the Source Code Editor, you manually create the code for your MIDlets. Creating code in the Source Code Editor gives you more flexibility when editing the code, and enables you to insert preprocessor code blocks. You are going to create a MyHello application using the New Project and New File wizards, and complete the code using the Source Editor.

Creating a New Java ME MIDP Project

  1. Choose File > New Project. Under Categories, select Java ME. Under Projects, select Mobile Application and click Next.
  2. Enter MyHelloMIDlet in the Project Name field (note that MID is in upper case letters). Specify the Project Location. We refer to this directory as $PROJECTHOME in this tutorial.
  3. Deselect the Create Default Package and Main Executable Class checkbox and click Next.
  4. Select the default emulator platform and use the remaining defaults. Click Finish.
    The IDE creates the $PROJECTHOME/MyHelloMIDlet project folder. The project folder contains all of your sources and project metadata, such as the project Ant script.
  5. Right-click the MyHelloMIDlet node in the Projects window and choose New > MIDlet.
  6. Enter HelloMIDlet as the MIDlet name (note that "MID" is not in upper case letters by default). Click Finish.
    The HelloMIDlet.java file is created.
  7. Double-click the HelloMIDlet.java file to display the source code in the IDE's Editor window.
  8. Click in the Source Editor and change public class HelloMIDlet extends MIDlet to
    public class HelloMIDlet
    extends MIDlet implements javax.microedition.lcdui.CommandListener
    {
  9. Add the following text before the startApp() method:
        private void initialize() {
            javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_helloTextBox());
        }
    
        public void commandAction(javax.microedition.lcdui.Command command, javax.microedition.lcdui.Displayable displayable) {
            if (displayable == helloTextBox) {
                if (command == exitCommand) {
                    javax.microedition.lcdui.Display.getDisplay(this).setCurrent(null);
                    destroyApp(true);
                    notifyDestroyed();
                }
            }
        }
    
        private javax.microedition.lcdui.TextBox get_helloTextBox() {
            if (helloTextBox == null) {
                helloTextBox = new javax.microedition.lcdui.TextBox(null, "Hello Test Code", 120, 0x0);
                helloTextBox.addCommand(get_exitCommand());
                helloTextBox.setCommandListener(this);
            }
            return helloTextBox;
        }
    
        private javax.microedition.lcdui.Command get_exitCommand() {
            if (exitCommand == null) {
                exitCommand = new javax.microedition.lcdui.Command("Exit", javax.microedition.lcdui.Command.EXIT,
                        1);
            }
            return exitCommand;
        }
        javax.microedition.lcdui.TextBox helloTextBox;
        javax.microedition.lcdui.Command exitCommand;
    
  10. Add a line initialize(); to the startApp() method, so it looks like the following:
      public void startApp() {
    initialize();
    }
    

Editing the Java Source Code

Now let's add some text for our MIDlet to display.

In the get_helloTextBox() method, replace the example Hello Test Code code with the text of your choice (for example, Make My Day.).

Note: For a complete guide to using code assistance features, customization options, and navigation capabilities of the NetBeans IDE's Java Editor see Code Assistance in the NetBeans IDE Java Editor.

Compiling and Running the Project

Choose Run > Run Project from the main IDE's toolbar. Follow the progress of the project compilation in the Output window. Note that the HelloMIDlet.java file is built before it is executed. A device emulator opens to display the results of the executed MIDlet and displays the text you entered in the source code.

'Make my day' message in Java ME SDK default emulator

To close the MIDlet and the emulator window, choose Application > Exit in the emulator window.

Creating a MIDP Application Using the Visual Mobile Designer

The NetBeans IDE provides a wizard that enables you to quickly create a MIDP project. When creating the project, you can choose to develop your application in the Visual Mobile Designer (VMD) or in the Source Code Editor. Using the VMD gives you the ability to visually plan out the flow of the application and design the screens the application uses. The designer automatically creates the code for the application as changes are saved on the design canvas.

Important: To run the Visual Mobile Designer in the IDE you need to install the Visual Mobile Designer plugin from the Update Center as follows:

  1. Choose Tools > Plugins from the main menu.
  2. On the Available Plugins tab of the Plugins dialog box, select Visual Mobile Designer and click Install.
  3. At the Welcome page of the Installer dialog box, click Next.
  4. At the License Agreement page, read the license agreements associated with the plugin. If you agree to the terms of all of the license agreements, click the appropriate option and then click Install.
  5. (Optional) Select the Run in the Background option to run the installation in the background.
  6. When the installation process is complete, click Finish.
  7. In the Plugins dialog box, click Close.

Creating a MIDP/CLDC Application

  1. Choose File > New Project (Ctrl+Shift+N). Under Categories, select Java ME. Under Projects, select Mobile Application and click Next.
  2. Enter MyHello in the Project Name field. Use the default Project Location, or change it to the directory you prefer on your system. In this tutorial we refer to this directory as $PROJECTHOME.
  3. Select the Create Default Package and Main Executable Class check box. Click Next.
  4. Select the default as the emulator platform and use the remaining defaults. Click Finish.
  5. The IDE creates the $PROJECTHOME/MyHello project folder. The project folder contains all of your sources and project metadata, such as the project Ant script. The application itself is displayed in the Flow Design window of the Visual Mobile Designer.

    Flow view of Hello Midlet in IDE

    Note: For a complete description of the available palette components, please refer to the Visual Mobile Designer Palette Reference.

Editing the Java Source Code

Now let's edit the text displayed by the MIDlet.

  1. Under HelloMIDlet.java click Screen to switch to the Screen View displaying the Device Screen, which is the only screen available in the application.
  2. Click the Hello World! message in the Device Screen. In the Properties window click the Hello World! text to change the welcome message text and press Enter. In this example we typed Make my day.

    The text 'Make my day' is typed in the text field of the Properties window.

  3. The Screen View displays a preview of the text you enter in the Text field.

    The text 'Make my day' is visible in Device Screen preview in the VMD's Screen view.

Compiling and Running the Project

  1. Choose Run > Run Project from the main IDE's toolbar. Follow the progress of the project compilation in the Output window. Note that the HelloMIDlet.java file is built before it is executed. A device emulator opens to display the results of the executed MIDlet. The device emulator launches the MIDlet and displays the text you entered in the source code.

    You can specify the emulator to run the MIDlet on by right-clicking the project node and selecting Run With.. from the contextual menu. The JavaMEPhone1 available in the Java ME SDK 3.2 is shown below.

    'Make my day' displayed in Java ME SDK default phone emulator

  2. In the emulator window, choose Application > Exit to close the MIDlet and the emulator window.

Next Steps

For more tutorials showing how to developing MIDP and CLDC applications visit the Mobile Application Learning trail.

get support for the NetBeans

Support


By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo