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); } }