Cahners Business Information Wireless Internet
|    Home    |    Archives    |    ASP Directory    |    Event Calendar    |    Subscribe    
Wireless Week
Topics

Features
Above the Fray
Follow the Money
Market Research
Big Ideas
Technoptions
Developer Insights
Q & A
Opinions

Tools and Services

Archives
Calendar of Events
ASP Directory
Other Cahners Pubs

About Us

Submit a Release
Meet our Staff
Contact Us
Advertise
Magazine Subscription

Powered by...

Cahners

QuoteMedia

MyPoll.net



Printer-friendly format

A Summertime Dive Into J2ME (Part 2)



In Part 1 of this article, you downloaded the Java 2 SDK and Sun's Java Wireless Toolkit. If both tools are installed and functioning correctly on your machine, we're ready to move forward!

Now it's time to write some code. All J2ME apps must be derived from the javax.microedition.midlet.MIDlet class, explaining why J2ME apps are often referred to as "midlets". The MIDlet class defines the core application setup functionality through the startApp(), pauseApp(), and destroyApp(). These classes are called by the J2ME runtime environment to manage the application's lifecycle. As you might expect, startApp() is called when the application begins or when it resumes from being paused, pauseApp() when the app is paused (or the environment switches to another application), and destroyApp() when the application is shutdown.

J2ME was designed from the ground up for small mobile devices and this is especially evident in the design of a typical J2ME application's user interface. Most mobile phones and paging devices include arrow keys for scrolling and one or more buttons to be used to select menu options on the current screen. J2ME supports this interface through the new javax.microedition.lcdui.Command class. Command objects interact with an application's javax.microedition.lcdui.Display object to present the user with menu choices. User selections are then handled through the implementation of the javax.microedition.lcdui.CommandListener interface. The CommandListener interface defines one method for trapping user events:

public void commandAction(Command c, Displayable d)

The following example makes use of the J2ME GUI elements found in the javax.microedition.lcdui package. It consists of two user screens, one containing a form object (which itself contains two TextField elements), and one containing a TextBox element that displays the user input from the first form. The code for this simple application is shown below, followed by a screenshot of the application running within a Motorola i85s phone emulator.


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


public class J2METester extends MIDlet implements CommandListener {
   Display display = null;
   Form frmMain = null;
   
   TextField txtName = null;
   TextField txtPassword = null;   
   TextBox txtResults = null;
   
   StringItem strName = null;
   StringItem strInfo = null;

   private Command cmdSubmit;
   private Command cmdBack;
   private Command cmdExit;

   public J2METester() {
       display = Display.getDisplay(this);   	
       
       cmdSubmit = new Command("Submit", Command.SCREEN, 1);	
       cmdBack = new Command("Back", Command.BACK, 0);
       cmdExit = new Command("Exit", Command.EXIT, 1);
   }

   public void startApp() throws MIDletStateChangeException {
      frmMain = new Form("J2METester");
      
      txtName = new TextField("Name", null, 50, TextField.ANY);
      txtPassword = new TextField("Password", null, 20, TextField.PASSWORD);       
      frmMain.append(txtName);
      frmMain.append(txtPassword);

      frmMain.addCommand(cmdSubmit);
      frmMain.addCommand(cmdExit);
      frmMain.setCommandListener(this);
      display.setCurrent(frmMain);
   }

   public void pauseApp()  {
      display = null;
   }

   public void destroyApp(boolean unconditional)  {
      notifyDestroyed();
   }

   public void commandAction(Command c, Displayable d)  {

      String str = c.getLabel();

      if (str.equals("Exit")) {
         destroyApp(true);
      }
      else if (str.equals("Back")) {
         display.setCurrent(frmMain);
      }
      else  {
      	 String strSubmit = "You entered:\nUserName: " + txtName.getString() + 
		"\nPassword: " + txtPassword.getString();
         txtResults = new TextBox("Results", strSubmit, 255, TextField.ANY);
         txtResults.addCommand(cmdBack);
         txtResults.setCommandListener(this);
         display.setCurrent(txtResults);
      }

   }
}


 
The J2METester Application

As you can see, this application creates two TextField objects and adds them to the frmMain Form object. When the user selects the "Submit" menu option, the TextField contents are retrieved and used to populate a TextBox object. This TextBox is then set to be the current item in the Display. Selecting the cmdBack Command will return the focus to frmMain.

This example should give you a good introduction to the flow and structure of a J2ME application. This same application will run unmodified (without a recompilation) on a rapidly growing number of mobile devices, including Palm OS PDAs, Motorola J2ME phones, and (soon) Nokia and RIM Blackberry devices. To get started with J2ME today, visit http://java.sun.com/j2me, download the Wireless Toolkit, and start coding!


Bryan Morgan is an independent writer and software developer. He is a columnist with Wireless Internet magazine and was the founder of WirelessDevNet.com in 1999.

 

Sponsors









|    Home    |    Archives    |    ASP Directory    |    Event Calendar    |    Subscribe    |

Copyright © 2001 Cahners Business Information
Use of this Web site is subject to its Terms of Use
Privacy Policy