Home > Articles > Programming > Java

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Mapping Command to a Specific Soft Button

Let's take a look at one last example, mapping a Command to a specific soft button. Following are the Command objects for this example:

  cmdHelp = new Command("Help", Command.HELP, 1);
  cmdBack = new Command("Back", Command.BACK, 1);
  cmdExit = new Command("Exit", Command.EXIT, 1);

On the Nokia emulator shown in Figure 8, the screenshot on the left shows the Exit command mapped to the soft button on the device. Accessing the Help command is accomplished by choosing the Options soft button and then selecting Help from the menu (see the right screenshot in Figure 8).

Figure 8 Help command mapped to a menu.

Once Help is selected from the menu, the TextBox shown in the left screenshot of Figure 9 is displayed. To exit the Help screen, select the Options soft button and choose Back from the menu (see the right screenshot in Figure 9).

Figure 9 Help textbox and Back command.

Figure 10 shows the same MIDlet when run on the Minimum Phone emulator available as part Sun's J2ME Wireless Toolkit.

Figure 10 Minimum Phone emulator with direct Back command mapping.

Given the screen limitations of this device, there are no commands shown directly on the display. On the leftmost screen shot in Figure 10, notice that there is soft button to engage the menu. Once it's visible, you'll find the commands Exit and Help. The rightmost screen shot shows the help text. This particular device has performed a direct mapping of the Back command to the soft button with the same name. Unlike with the Nokia device, we do not need to access a menu to leave the Help screen; we simply select the soft button labeled Back. (See Listing 3.)

Listing 3: MappingCommands.java

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

public class MappingCommands extends MIDlet implements CommandListener
 private Display display;   // Reference to Display object for this MIDlet
 private Form fmMain;     // The main Form
 private TextBox tbHelp;    // A Textbox to display a help message
 private Command cmExit;    // A Command to exit the MIDlet
 private Command cmHelp;    // A Command to ask for Help
 private Command cmBack;    // A Command to go "back" to the main form

 public MappingCommands()
  display = Display.getDisplay(this);

  cmHelp = new Command("Help", Command.HELP, 1);
  cmBack = new Command("Back", Command.BACK, 1);
  cmExit = new Command("Exit", Command.EXIT, 1);

  // Create the Form, add Commands, listen for events
  fmMain = new Form("Core J2ME");

  // Create the help Textbox with a maximum of 25 characters
  tbHelp = new TextBox("Help", "Help text here...", 25, 0);


 // Called by application manager to start the MIDlet.
 public void startApp()

 // A required method
 public void pauseApp()
 { }

 // A required method
 public void destroyApp(boolean unconditional)
 { }

 // Process events
 public void commandAction(Command c, Displayable s)
    if (c == cmExit)
    else if (c == cmHelp)
    else if (c == cmBack)
  • + Share This
  • 🔖 Save To Your Account