corner imagecorner image
FeaturesPluginsDocs & SupportCommunityPartners

NetBeans CLDC/MIDP Development Quick Start Guide

This document takes you through the basics 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 going 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.0 and 6.1

Requirements

You must have JDK 5 or better and the NetBeans IDE Mobility or Full edition (download) installed before you can start Java ME MIDP/CLDC development. For help getting your system set up, please see the installation instructions. MacOS X users should refer to the MIDP development on Mac OS set up guide.

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 Visual Mobile Designer gives you the ability to graphically plan out the flow of the application and design the screens the application uses. The designer automatically creates the code for the application.

Creating a MIDP/CLDC Application

  1. Choose File > New Project (Ctrl+Shift+N). Under Categories, select Mobility. Under Projects, select MIDP 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 Sun Java Wireless Toolkit 2.52 for CLDC as the Emulator Platform and use the remaining defaults. Click Next.
  5. Click Finish. 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.
  6. 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. Click on Screen. This opens the Screen Designer window, and displays the Device screen, which is the only screen available in the application.
  2. In the Properties window, click in the Text field and type in some new text. 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 default device emulator is DefaultColorPhone.
  2. In the device emulator window, click on the button below the Launch command. The device emulator launches the MIDlet and displays the text you entered in the source code.
  3. Make my day message displayed in WTK emulator

  4. 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 Mobility. Under Projects, select MIDP Application and click Next.
  2. Enter MyHelloMIDlet in the Project Name field (note that "MID" is in upper case letters). Use the default Project Location, or change it to the directory you prefer on your system. 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 Sun Java Wireless Toolkit 2.52 for CLDC as the Emulator Platform and use the remaining defaults. Click Next.
  5. Expand "Configuration templates provided by installed CLDC platforms" and "Sun Java Wireless Toolkit 2.52 for CLDC " folders. Check the boxes next to each of the configurations. The IDE automatically creates a new project configuration for each template listed.
  6. 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.
  7. Right-click the MyHelloMIDlet node in the Explorer window and choose New > MIDlet
  8. Enter HelloMIDlet as the MIDlet name (note that "MID" is 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.
  9. Click in the Source Editor and change
    public class HelloMIDlet extends MIDlet
    to
    public class HelloMIDlet
    extends MIDlet implements javax.microedition.lcdui.CommandListener
    {
  10. 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, "Make My Day", 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;
    
  11. 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 "test string" code with the text of your choice. For example, "Make my day."

Note: For a complete guide to using the Java Source Editor in NetBeans see Java Editing in NetBeans IDE.

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 default device emulator is DefaultColorPhone.
  2. In the device emulator window, click on the button below the Launch command. The device emulator launches the HelloMIDlet and displays the text you entered in the source code.
  3. Make my day message displayed in WTK emulator


Next Steps

The next step to developing MIDP and CLDC applications is knowing how to use multiple device emulators in the IDE and create custom configurations that allow a project to be deployed to multiple devices.

Bookmark this page

del.icio.us furl simpy slashdot technorati digg