×

Creating your first LCDUI MIDlet

This topic aims to help you create your first LCDUI MIDlet. The following list of steps demonstrate how to create a simple HelloWorld LCDUI MIDlet that displays the Hello World label using the LCDUI form.

Assumption

This guide assumes that you are already using Nokia Asha SDK 1.2 or older. If not refer to, Nokia Asha overview page

Steps

  1. From the Start menu, select Nokia Asha SDK > Nokia IDE for Java ME (Eclipse) v3.1. The Workspace Launcher dialog appears.

    Figure 1. Selecting the workspace

  2. Click OK to use the default workspace. This launches Nokia IDE for Java ME (Eclipse).

  3. Create a new MIDlet project by selecting File > New > MIDlet Project. The New MIDlet project window appears.

  4. Specify HelloWorldLCDUI in the Project name field and click Finish.

    Figure 2. Creating a MIDlet project

    The newly created MIDlet project appears on Nokia IDE for Java ME (Eclipse).

    Figure 3. HelloWorldLCDUI MIDlet project on Nokia IDE for Java ME (Eclipse) v3.1

  5. In the Package Explorer view, right-click on HelloWorldLCDUI > src and select New > Java ME MIDlet. The New Java ME MIDlet dialog appears.

    Figure 4. Creating a Java ME MIDlet

  6. Specify the following details in the New Java ME MIDlet window and click Finish:

    • Package: A package name for your Java ME MIDlet, for example, com.example.helloworld.

    • Name: A name for your Java ME MIDlet, for example, HelloWorldMIDlet.

    Figure 5. Specifying the Java ME MIDlet details

    The HelloWorldMIDlet.java is loaded into the workspace.

    Figure 6. HelloWorldMidlet.java is loaded into the workspace

  7. In the Package Explorer view, right-click on HelloWorldLCDUI > src > com.example.helloworld and select New > Class. The New Java Class dialog appears.

    Figure 7. Creating the HelloScreen class

  8. In the New Java Class dialog, specify the following details and click Finish:

    • Package: A package name for your Java ME MIDlet, for example, com.example.helloworld.

    • Name: A name for your Java class, for example, HelloScreen.

    Figure 8. New Java Class dialog

    The HelloScreen class is loaded into your workspace.

    Figure 9. HelloScreen class loaded into your workspace

  9. Replace the existing code of HelloScreen.java with the following code and save your changes. This code implements the LCDUI form that displays the label Hello World.

    package com.example.helloworld;
    
    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();
            }
        }
    }

    Figure 10. Updated HelloScreen.java source code

  10. Add the following import statements after package com.example.helloworld; in HelloWorldMIDlet.java. This imports the required LCDUI components required for the MIDlet.

    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Displayable;
    

    Figure 11. Importing LCDUI components

  11. Replace the existing code of startApp() method in HelloWorldMIDlet.java with the following code and save your changes. This code displays the HelloScreen LCDUI form with the text Hello World.

    // Sets the MIDlet's current Display to a HelloScreen object.
        public void startApp() throws MIDletStateChangeException {
            Displayable current = Display.getDisplay(this).getCurrent();
            if (current == null) {
                HelloScreen helloScreen = new HelloScreen(this, "Hello, world!");
                Display.getDisplay(this).setCurrent(helloScreen);
            }
        }

    Figure 12. Updating startApp() method code

    Figure 13. Updated HelloWorldMidlet.java source code

  12. Run the MIDlet project by right-clicking on the project and selecting Run As > Emulated Java ME MIDlet.

    Figure 14. Running the MIDlet project

    The MIDlet project output is displayed on the emulator as follows.

    Figure 15. HelloWorld output

Next steps


Last updated 5 March 2014

Back to top

×