Skip to main content
IBM 
ShopSupportDownloads
IBM HomeProductsConsultingIndustriesNewsAbout IBM
IBM : developerWorks : Java Technology : Education - online courses
J2ME: Step by step
Download tutorial zip fileView letter-sized PDF fileView A4-sized PDF fileE-mail this tutorial to a friend
Main menuSection menuGive feedback on this tutorialPreviousNext
10. MIDP API
  


The HelloWorld MIDlet page 5 of 6


As with the KJava HelloWorld application, this MIDlet also displays, "Hello World!" on the screen of an MIDP device, as well as an Exit button, which terminates the application when pressed.

The HelloWorld.java file starts with the following lines of code to import the classes that will be used later in the HelloWorld class:


      import javax.microedition.midlet.MIDlet;
      import javax.microedition.lcdui.Command;
      import javax.microedition.lcdui.CommandListener;
      import javax.microedition.lcdui.Display;
      import javax.microedition.lcdui.Displayable;
      import javax.microedition.lcdui.Form;

The HelloWorld class extends MIDlet since it is an MIDP application. It also implements CommandListener interface to handle events:


      public class HelloWorld extends MIDlet implements CommandListener

The following method is the default constructor, which creates a new form, initializes the controls on it, and then displays it:


      private Form form;

      public HelloWorld()
      {
          // Create a new form on which to display our text
         form = new Form("Test App");

         // Add the text "Hello World!" to the form
         form.append("Hello World!");

         // Add a command button labeled "Exit"
         form.addCommand( new Command( "Exit", Command.EXIT, 1 ) );

         // Register this object as a commandListener
         form.setCommandListener( this );
      }              

The startApp() method is invoked to start the application much like an applet's start method. It may be called numerous times during a single execution of a MIDlet. If the MIDlet is paused, pauseApp() will be invoked. To restart the MIDlet, startApp() will be called. Main initialization code that needs to be performed only once should be placed in the constructor:


      public void startApp()
      {
         // Get a reference to the display, and show the form
         Display display = Display.getDisplay(this);
         display.setCurrent( form );
      } 

pauseApp() is invoked to put the MIDlet into a paused state. In this application, we do nothing when entering the paused state; however, we must still implement the pauseApp method in our MIDlet because it is an abstract method in the parent MIDlet class.


      public void pauseApp() { }

destroyApp() is invoked to destroy the MIDlet and put it into a destroyed state. In this application, we release our reference to the form by setting it to null.

   
      public void destroyApp(boolean unconditional)
      {
         form = null;
      }

The commandAction() method is the event handler required to implement the CommandListener interface. Currently it destroys the application, and notifies the application management software that the MIDlet is complete.


      public void commandAction(Command c, Displayable d)
         {
            // Destroy this MIDlet
            destroyApp(true);

            // Notify the application management software that this MIDlet
            // has entered the destroyed state
            notifyDestroyed();
         }

Main menuSection menuGive feedback on this tutorialPreviousNext
PrivacyLegalContact