IconCommand

The IconCommand class extends the LCDUI Command class by allowing MIDlets to provide an icon to a Command. On full touch Series 40 devices, MIDlets can use IconCommands to map an icon to action button 1 and place icons in the CategoryBar.

Figure: IconCommand with a custom icon on Action Button 1 (Series 40 full touch)

In general, MIDlets can use IconCommands anywhere they can use Commands. (Since IconCommand is a subclass of Command, an IconCommand instance can always substitute a Command instance.) In Nokia Asha software platform devices, the IconCommand can only be used to place up to four icons in the CategoryBar.

The IconCommand class is supported on Nokia Asha software platform devices with Java Runtime for Nokia Asha software platform devices and Series 40 devices with Java Runtime 2.0.0 for Series 40 or newer.

An IconCommand differs from a Command only in terms of its visual appearance. While a Command is represented only by a text label, an IconCommand is represented by both a text label and an icon. The image data for the icon can come from a built-in system icon described using a unique ID or from image data provided by the MIDlet. When a MIDlet provides its own image data for an icon, it can provide both the selected and unselected versions of the image. However, only the unselected version of the icon image is mandatory.

MIDlets can dynamically change some IconCommand properties. If the IconCommand is visible when its property is changed, the change takes effect immediately. If the IconCommand is not visible, the change takes effect when the IconCommand next becomes visible.

Figure: IconCommands in CategoryBar

Creating an IconCommand

To create an IconCommand:

  1. Create a new IconCommand object. Use the constructor parameters to define the icon image, labels, command type, and priority.
    IconCommand okcommand;
    IconCommand screencommand;
    
    //Image provided to represent selected icon
    img = Image.createImage("/image/selected.png");
    
    //Image provided to represent unselected icon
    unselected_img = Image.createImage("/image/unselected.png");
    
    //Create the IconCommand using system icon ID
    okcommand = new IconCommand("Ok", "Okay", Command.OK, 1, IconCommand.ICON_OK);
    
    //Create the IconCommand using midlet defined images 
    screencommand = new IconCommand("Screen", "Screen Command", unselected_img, img, Command.SCREEN, 2);
    
    
  2. Define the action for the IconCommand by using a CommandListener. Like a Command, an IconCommand only encapsulates the semantic information for an action, not the actual action, so you need to implement the action separately through a CommandListener.

For detailed information about the IconCommand constructors and built-in system icon IDs, see the IconCommand class reference.

Retrieving the properties of an IconCommand

You can retrieve the following properties from an existing IconCommand:

Code example: Using IconCommand

The following code example shows you how to use IconCommands.

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

import com.nokia.mid.ui.*;

/**
 * adding and removing of commands in a form
 */
public class FTCMT03 extends MIDlet implements CommandListener, ItemCommandListener
{
  // Command names to be added or removed
  private StringItem  ok;
  private StringItem  help;
  private StringItem  screen;
  private StringItem  stop;
  private StringItem  cancel;
  private StringItem  exit;
  private StringItem  item;
  private StringItem  back;

  // add and remove commands
  private Command     addcommand;
  private Command     removecommand;

  // Main display form
  private Form        mainform;
  Display             disp;

  // All commands
  private IconCommand okcommand;
  private IconCommand helpcommand;
  private IconCommand screencommand;
  private IconCommand stopcommand;
  private IconCommand cancelcommand;
  private IconCommand exitcommand;
  private IconCommand itemcommand;
  private IconCommand backcommand;

  public FTCMT03()
  {
    ok = new StringItem("Command type", "OK");
    help = new StringItem("Command type", "help");
    screen = new StringItem("Command type", "Screen");
    stop = new StringItem("Command type", "Stop");
    cancel = new StringItem("Command type", "Cancel");
    exit = new StringItem("Command type", "Exit");
    item = new StringItem("Command type", "Item");
    back = new StringItem("Command type", "Back");

    addcommand = new Command("Add", Command.ITEM, 1);
    removecommand = new Command("remove", Command.ITEM, 2);

    mainform = new Form("Add/Remove Commands");
    disp = Display.getDisplay(this);

    okcommand = new IconCommand("OK", "okay", Command.OK, 1, IconCommand.ICON_OK);
    helpcommand = new IconCommand("Help", "Help command", Command.HELP, 1, IconCommand.ICON_ADD_CONTACT);
    screencommand = new IconCommand("Screen", "screen command", Command.SCREEN, 1, IconCommand.ICON_OPTIONS);
    stopcommand = new IconCommand("Stop", "Stop command", Command.STOP, 1, IconCommand.ICON_BACK);
    cancelcommand = new IconCommand("Cancel", "cancel command", Command.CANCEL, 1, IconCommand.ICON_BACK);
    exitcommand = new IconCommand("exit", "exit command", Command.EXIT, 1, IconCommand.ICON_BACK);
    itemcommand = new IconCommand("Item", "item command", Command.ITEM, 1, IconCommand.ICON_OPTIONS);
    backcommand = new IconCommand("back", "back command", Command.BACK, 1, IconCommand.ICON_OPTIONS);

  }

  public void startApp()
  {
    showMainDisplay();
  }

  public void pauseApp()
  {
  }

  public void destroyApp(boolean unconditional)
  {
  }

  public void commandAction(Command c, Displayable d)
  {
    if (c == backcommand)
    {
      Alert alert = new Alert("Back");
      Display.getDisplay(this).setCurrent(alert);
    }
    else if (c == okcommand)
    {
      Alert alert = new Alert("OK");
      Display.getDisplay(this).setCurrent(alert);
    }
    else if (c == helpcommand)
    {
      Alert alert = new Alert("Help");
      Display.getDisplay(this).setCurrent(alert);
    }
    else if (c == screencommand)
    {
      Alert alert = new Alert("Screen");
      Display.getDisplay(this).setCurrent(alert);
    }
    else if (c == stopcommand)
    {
      Alert alert = new Alert("Stop");
      Display.getDisplay(this).setCurrent(alert);
    }
    else if (c == cancelcommand)
    {
      Alert alert = new Alert("Cancel");
      Display.getDisplay(this).setCurrent(alert);
    }
    else if (c == exitcommand)
    {
      Alert alert = new Alert("Exit");
      Display.getDisplay(this).setCurrent(alert);
    }
    else if (c == backcommand)
    {
      Alert alert = new Alert("Item");
      Display.getDisplay(this).setCurrent(alert);
    }

  }

  public void commandAction(Command c, Item i)
  {
    if (c == addcommand)
    {
      if (i == ok)
      {
        mainform.addCommand(okcommand);
        mainform.setCommandListener(this);
      }
      else if (i == help)
      {
        mainform.addCommand(helpcommand);
        mainform.setCommandListener(this);
      }
      else if (i == screen)
      {
        mainform.addCommand(screencommand);
        mainform.setCommandListener(this);
      }
      else if (i == stop)
      {
        mainform.addCommand(stopcommand);
        mainform.setCommandListener(this);
      }
      else if (i == cancel)
      {
        mainform.addCommand(cancelcommand);
        mainform.setCommandListener(this);
      }
      else if (i == exit)
      {
        mainform.addCommand(exitcommand);
        mainform.setCommandListener(this);
      }
      else if (i == item)
      {
        item.addCommand(itemcommand);
        mainform.setCommandListener(this);
      }
      else if (i == back)
      {
        mainform.addCommand(backcommand);
        mainform.setCommandListener(this);
      }

    }
    else if (c == removecommand)
    {
      if (i == ok)
      {
        mainform.removeCommand(okcommand);
      }
      else if (i == help)
      {
        mainform.removeCommand(helpcommand);
      }
      else if (i == screen)
      {
        mainform.removeCommand(screencommand);
      }
      else if (i == stop)
      {
        mainform.removeCommand(stopcommand);
      }
      else if (i == cancel)
      {
        mainform.removeCommand(cancelcommand);
      }
      else if (i == exit)
      {
        mainform.removeCommand(exitcommand);
      }
      else if (i == item)
      {
        item.removeCommand(itemcommand);
      }
      else if (i == back)
      {
        mainform.removeCommand(backcommand);
      }

    }

  }

  public void showMainDisplay()
  {
    setStringItems();
    mainform.append(ok);
    mainform.append(help);
    mainform.append(screen);
    mainform.append(stop);
    mainform.append(cancel);
    mainform.append(exit);
    mainform.append(item);
    mainform.append(back);
    // mainform.setCommandListener(this);
    disp.setCurrent(mainform);

  }

  public void setStringItems()
  {
    ok.addCommand(addcommand);
    ok.addCommand(removecommand);
    ok.setItemCommandListener(this);

    help.addCommand(addcommand);
    help.addCommand(removecommand);
    help.setItemCommandListener(this);

    screen.addCommand(addcommand);
    screen.addCommand(removecommand);
    screen.setItemCommandListener(this);

    stop.addCommand(addcommand);
    stop.addCommand(removecommand);
    stop.setItemCommandListener(this);

    cancel.addCommand(addcommand);
    cancel.addCommand(removecommand);
    cancel.setItemCommandListener(this);

    exit.addCommand(addcommand);
    exit.addCommand(removecommand);
    exit.setItemCommandListener(this);

    item.addCommand(addcommand);
    item.addCommand(removecommand);
    item.setItemCommandListener(this);

    back.addCommand(addcommand);
    back.addCommand(removecommand);
    back.setItemCommandListener(this);

  }
}

More information

For example of MIDlets that show you how to use IconCommands, see: