How to Write a Java MIDlet Program for Wireless Cellular Phones and Handheld PDAs Using J2ME

Abstract

This article describes how to generate PRC software files for a Palm Pilot PDA that supports java. This describes how to create .jar and .jad files, often referred to as MIDlets, that could be converted to PRC files using J2ME (Java 2 Micro Edition) Wireless Tool kit and Java source files. Such PRC files created by J2ME Wireless Toolkit can be easily installed and run on a Palm PDA like any other application by using the HotSync Manager.

Procedures

  1. Download and install J2SE (Java 2 Platform, Standard Edition) from the Sun Developer Network Web site.
  2. Download and install J2ME WTK (Java 2 Platform, Micro Edition Wireless Tool Kit) from the Sun Developer Network Web site. J2SE needs to be installed prior to the J2ME WTK installation.
  3. Create a J2ME Java source file on any basic editor such as Notepad or Wordpad and save the file with a .java extension.
  4. Run the J2ME WTK (wireless toolkit) application program available in the bin subdirectory of the J2ME WTK install folder; for example, if J2ME WTK is installed in c:\WTK104, the application program is available at c:\WTK104\bin\ktoolbar.exe.ktoobar.exe. It looks like Figure 1 when it runs.
  5. Figure 1

  6. Select the New project button and type in a project name for the MIDlet program. Type the MIDlet class name as exactly the name of the Java class that is going used by this project as shown in Figure 1 and click the Create Project button to create a new project. A new project, named SampleProject, will be created in the apps sub folder of the J2ME install directory.
  7. Here is a sample j2me source file:

    //SampleMidlet.java
    
    //Sample midlet program that could be converted to PRC files
    //that install into a Palm PDA.
    
    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.*;
    import javax.microedition.io.*;
    
    import java.io.* ;
    import java.lang.*;
    
    public class SampleMidlet extends MIDlet implements
           CommandListener
    {
    //member variables
    
    private Display dCurrentDisplay;
    private Form mMainMenu;
    private List lChooselist;
    private Ticker tMainTicker;
    private TextBox txtInputBox;
    private DateField dDate = new DateField("Today : ",
                                            DateField.DATE);
    
    //commands used, such as button, labels, and screens
    
    static final Command cBack =
           new Command("Back",Command.BACK,0);    //back button
    static final Command cExit =
           new Command("Exit",Command.STOP,2);    //Exit button
    static final Command cTransfer =
           new Command ("Transfer",Command.SCREEN,3);
    
    //variables
    
    String CurrentMenu = null;
    String Text        = null;
    
    //constructors
    
    public SampleMidlet()
    {
       mMainMenu = new Form("");
       tMainTicker = new Ticker("Welcome To Sample MIDlet");
    
    }
    //-----------------------------------------------------------
    
    //start application method -- called initailly while running
    
    public void startApp() throws MIDletStateChangeException
    {
       dCurrentDisplay = Display.getDisplay(this);
       lChooselist     = new List("",Choice.IMPLICIT);
       lChooselist.append("Say Hello",null);
       lChooselist.append("Get Input",null);
       lChooselist.append("Print Input",null);
       lChooselist.addCommand(cExit);
       lChooselist.setCommandListener(this);
       lChooselist.setTicker(tMainTicker);
       mainmenu();
    }
    //-----------------------------------------------------------
    
    //mainmenu method to display the option list
    
    void mainmenu()
    {
       dCurrentDisplay.setCurrent(lChooselist);
       CurrentMenu = "Main";
    }
    //-----------------------------------------------------------
    //PauseApp method to stop the program
    
    public void pauseApp()
    {
       dCurrentDisplay = null;
       lChooselist     = null;
       tMainTicker     = null;
       mMainMenu       = null;
       txtInputBox     =null;
    }
    //-----------------------------------------------------------
    
    public void ShowHello()
    {
       txtInputBox = new TextBox(" ","",50,TextField.ANY);
       txtInputBox.addCommand(cBack);
       txtInputBox.setCommandListener(this);
       txtInputBox.setString("HELLO WORLD");
       dCurrentDisplay.setCurrent(txtInputBox);
       CurrentMenu = "Hello";
    }
    //-----------------------------------------------------------
    public void GetInput()
    {
       txtInputBox = new TextBox("Enter Text:
                                 ","",50,TextField.ANY);
       txtInputBox.addCommand(cBack);
       txtInputBox.setCommandListener(this);
       txtInputBox.setString(" ");
       dCurrentDisplay.setCurrent(txtInputBox);
       CurrentMenu = "Input";
    
    }
    //-----------------------------------------------------------
    public void PrintInput()
    {
    
       txtInputBox = new TextBox("Here is what You typed",
                                 "",50,TextField.ANY);
       txtInputBox.addCommand(cBack);
       txtInputBox.setCommandListener(this);
       txtInputBox.setString(Text);
       dCurrentDisplay.setCurrent(txtInputBox);
       CurrentMenu = "Print";
    
    }
    //-----------------------------------------------------------
    public void destroyApp(boolean unconditional)
    {
       notifyDestroyed();
    }
    //-----------------------------------------------------------
    
    //Handle List Events
    
    public void commandAction(Command c, Displayable d)
    {
       String sLabel = c.getLabel();
       if(sLabel.equals("Exit"))
       {
          destroyApp(true);
       }
       else if(sLabel.equals("Back"))
       {
          if(CurrentMenu.equals("Input"))
          {
             Text = txtInputBox.getString();
             mainmenu();
          }
          if(CurrentMenu.equals("Hello"))
             mainmenu();
          if(CurrentMenu.equals("Print"))
             mainmenu();
       }
       else
       {
    
          List nIndex = (List)dCurrentDisplay.getCurrent();
          switch(nIndex.getSelectedIndex())
          {
             case 0:ShowHello();
                break;
             case 1:GetInput();
                break;
             case 2:PrintInput();
                break;
             default:break;
          }
       }
    }
    //-----------------------------------------------------------
    
    }    //end program
    

    J2ME displays a message saying a new project is created and a folder named SampleProject will be created in the apps subdirectory in the install folder.

  8. Once the project is created, copy the source Java file, in this case SampleMidlet.java, in the src subdirectory of the SampleProject folder. Now, open the project in J2ME WTK using the button Open Project; once the project is loaded, press the Build button to build the project. If there is no syntax error, the project will build successfully. After the build, choose the Project | Package |Create Package options from the main menu to build the .jar and .jar files for the project, as shown in Figure 2.
  9. Figure 2

    On success, the SampleProject.jar and SampleProject.jad files will be created in the bin subdirectory of the SampleProject folder.

  10. Once built, the project could be run by using the J2ME default emulator device or by choosing the Palm emulator from the Device Combo box, as shown in Figure 2. The Palm OS Emulator (POSE) needs to be downloaded from Palm Developer Web site and installed to choose the Palm emulator as a default device in J2ME WKT. This project could be tested with the default device that comes with J2ME, which is a common cell phone image. But, the SampleProject.jad and SampleProject.jar will have to be converted to SampleProject.prc to install into a Palm Pilot device using the Hot Sync Manager. Here is how it looks if this project runs in the Palm emulator, as shown Figure 3.
  11. Figure 3

  12. To generate the PRC file, choose the File | Utilities options and select PalmOSEmulator. Press the Generate PRC button to run the Sun PRC Tool Converter, as shown in Figure 4, to run the PRC Tool Convertor, as shown in Figure 5. Choose File | Convert and choose the SampleProject folder to convert the SampleProject.jar and SampleProject.jad into the SampleProject.prc file that will be copied to the bin subdirectory into the SampleProject folder.
  13. Figure 4

    Figure 5

  14. The PRC file generated from J2ME WTK can be installed easily into the Palm device by using the Hot Sync Manager. Once installed, this application appears on the Palm Applications screen and can be run by tapping with the stylus on the icon if the MIDP.prc (MIDP for Palm OS) is installed in the Palm Device. MIDP for Palm OS is available for download from Sun Developer Network Web site. Once MIDP is downloaded and installed, MIDP.prc is available in the PRC files subdirectory of the install folder to be installed into the Palm Device by using the Hot Sync manager.

Conclusion

This article focuses on creating MIDlets and converting MIDlet files into PRC files to be downloaded to the Palm Device. The above described SampleProject J2ME MIDlet package could be easily converted and installed to any modern wireless cellular or other PDAs available on the market today as long as it supports J2ME and MIDP (Mobile Information Device Profile) from Sun Microsystems; such items are available from Motorola, Nokia, Samsung, Panasonic, and so forth.

References

  1. Sun Microsystems Web site: http://www.sun.com.
  2. Sun Microsystems Developers Web site: http://developers.sun.com.
  3. Palm Developer’s Network Web site: http://www.palm.com.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read