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.

Contents

Content on this page applies to NetBeans IDE 6.9 or later

Requirements

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

Software or Resource Version Required
NetBeans IDE with Java ME Version 6.9 or later
Java Development Kit (JDK) Version 6 or 7

Note: The emulator bundled with the IDE varies depending on which operating system installer is downloaded.

  • The Windows distribution of NetBeans 6.7 and later comes bundles with the Java ME SDK 3.0.
  • The Linux distribution of NetBeans IDE is bundled with the Sun Java Wireless Toolkit 2.5.2.
  • The MacOS X version of NetBeans 6.9 is bundled with the Java ME SDK 3.0 for CLDC.

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.

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. Check the Set as Main Project and Create Hello MIDlet check boxes (both are checked by default). 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 show the Screen Designer window diplaying 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.
  3. The text 'Make my day' is typed in the text field of the Properties window.

  4. The Screen view displays a preview of the text you enter in the Text field.
  5. 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 Main Project (F6) from the Run menu. 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.
  2. Tip: 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 DefaultFXPhone1 available in the Java ME SDK 3.0 is shown below.

    Make my day message displayed in Java ME SDK JavaFX phone emulator

  3. Click on the button below Exit to close the MIDlet. Then click on the button in the upper right corner of the device to close the emulator window.

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. Next we create the 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 (Ctrl-Shift-N). 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 that is different from the one you specified for the MyHello project created earlier. We refer to this directory as $PROJECTHOME in this tutorial.
  3. Check the Set as Main Project checkbox and remove the check from the Create Hello MIDlet checkbox. Click Next.
  4. Select the default emulator platform and use the remaining defaults. Click Finish.
  5. 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.
  6. Right-click the MyHelloMIDlet node in the Explorer window and choose New > MIDlet.
  7. 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 and the source code is displayed 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.

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

Compiling and Running the Project

  1. Choose Run > Run Main Project (F6) from the Run menu. 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.
  2. Make my day message displayed in Java ME SDK 3.0 CLDC Clamshell emulator


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