Home > Articles > Programming > Java

  • Print
  • + Share This

Getting Down to Business

At this point, I've discussed everything about J2ME except actual code! For the remainder of this article, I'll focus on a simple "Hello World" app that should prepare you for the development of a "real" app in Part 3 of this series. The CLDC and MID profile give you several packages to work with, including the following:

  • java.io—a subset of the J2SE package of the same name.

  • java.lang—a subset of the J2SE package of the same name.

  • java.util—a subset of the J2SE package of the same name.

  • javax.microedition.io—includes networking support based on the GenericConnection framework from the CLDC. Also includes the HttpConnection interface for HTTP protocol access.

  • javax.microedition.lcdui—a user-interface API tuned for MID devices.

  • javax.microedition.midlet—defines MIDP applications and the interactions between the applications and the environment in which they run.

  • javax.microedition.rms—provides a mechanism for midlets to persistently store data and later retrieve it.

As you can see, this set of packages includes some powerful capabilities: support for threads, exception handling, utility functions/data structures, stream I/O, GUI components, and a record store for application data storage!

The code for the "Hello World" midlet is shown in the following listing. You begin by acquiring the display context. From there, you simply add a text box (with the text "Hello World!" displayed) and an Exit button. A command listener is built in order to handle the Exit button selection event. When it's received, destroyApp() is called to shut things down. The purpose of showing this simple app is to demonstrate the lifecycle of a typical midlet. The application GUI and "internals" are set up within the startApp() method. The pauseApp() method is typically used to restart threads, retrieve updated data, or perform other time-related tasks. destroyApp() is called when the application is being shut down. Figure 5 shows the midlet being run in a Sun Java Wireless Toolkit emulator.

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class HelloWorldMIDLet extends MIDlet
        implements CommandListener {

  private Command exitCommand;
  private Display display;

  public HelloWorldMIDLet() {
    display = Display.getDisplay(this);
    exitCommand = new Command("Exit", Command.EXIT, 1);

  public void startApp() {
   TextBox txt = new TextBox("InformIT.com", "Hello World!", 256, 0);

  public void pauseApp() { }

  public void destroyApp(boolean unconditional) { }

  public void commandAction(Command c, Displayable s) {
    if (c == exitCommand) {

Figure 5

The "Hello World" midlet.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.