Multiple Commands In A Midlet
I was recently asked what difference would it make if I chose between the command types Command.SCREEN, Command.ITEM and Command.OK in a screen in a midlet with multiple menu items appearing in way similar to the screenshot below.

Figure 1 : Midlet Screen With Multiple Menu Items
Some simple code (below) that produces the above effect.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class MutipleCommandMidlet extends MIDlet implements CommandListener { private Command exitCmd, menuItem1, menuItem2, menuItem3, menuItem4, menuItem5, menuItem6, menuItem7, menuItem8; private Form form; public MutipleCommandMidlet() { exitCmd = new Command("Exit", Command.EXIT, 1); menuItem1 = new Command("Item 1", Command.SCREEN, 2); menuItem2 = new Command("Item 2", Command.SCREEN, 3); menuItem3 = new Command("Item 3", Command.SCREEN, 4); menuItem4 = new Command("Item 4", Command.SCREEN, 5); menuItem5 = new Command("Item 5", Command.SCREEN, 6); menuItem6 = new Command("Item 6", Command.SCREEN, 7); menuItem7 = new Command("Item 7", Command.SCREEN, 8); menuItem8 = new Command("Item 8", Command.SCREEN, 9); form = new Form("Some Form"); } public void commandAction(Command c, Displayable d) { String label = c.getLabel(); if (label.startsWith("Menu Item")) showAlert("SCREEN Command", label + " Pressed"); else if (label.equals("Exit")) showAlert("EXIT Command", label + " Pressed"); } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { /* not implemented */ } protected void pauseApp() { /* not implemented */ } private void showAlert(String alertTitle, String alertText) { System.out.println(alertTitle + " : " + alertText); Alert alert = new Alert(alertTitle, alertText, null, AlertType.INFO); alert.setTimeout(2000); Display.getDisplay(this).setCurrent(alert, form); } protected void startApp() throws MIDletStateChangeException { // Note that it does not matter which order the menu items are added form.addCommand(menuItem1); form.addCommand(menuItem3); form.addCommand(menuItem2); form.addCommand(menuItem4); form.addCommand(menuItem5); form.addCommand(menuItem6); form.addCommand(menuItem7); form.addCommand(menuItem8); form.addCommand(exitCmd); form.setCommandListener(this); Display.getDisplay(this).setCurrent(form); }// end-of-startApp } |
Take note of lines 11 to 20 and also lines 47 to 55
11 12 13 14 15 16 17 18 19 | exitCmd = new Command("Exit", Command.EXIT, 1); menuItem1 = new Command("Item 1", Command.SCREEN, 2); menuItem2 = new Command("Item 2", Command.SCREEN, 3); menuItem3 = new Command("Item 3", Command.SCREEN, 4); menuItem4 = new Command("Item 4", Command.SCREEN, 5); menuItem5 = new Command("Item 5", Command.SCREEN, 6); menuItem6 = new Command("Item 6", Command.SCREEN, 7); menuItem7 = new Command("Item 7", Command.SCREEN, 8); menuItem8 = new Command("Item 8", Command.SCREEN, 9); |
47 48 49 50 51 52 53 54 55 | form.addCommand(menuItem1); form.addCommand(menuItem3); form.addCommand(menuItem2); form.addCommand(menuItem4); form.addCommand(menuItem5); form.addCommand(menuItem6); form.addCommand(menuItem7); form.addCommand(menuItem8); form.addCommand(exitCmd); |
Now if we were to change the ordering in which the Commands are being added to the Form, to say something like this:
47 48 49 50 51 52 53 54 55 | form.addCommand(menuItem5); form.addCommand(menuItem4); form.addCommand(menuItem1); form.addCommand(menuItem3); form.addCommand(menuItem2); form.addCommand(menuItem6); form.addCommand(menuItem7); form.addCommand(menuItem8); form.addCommand(exitCmd); |
we would still get the screen looking like Figure 1 above. The reason (I think) this happens is because of the priorities (5 and 6 respectively) assigned to menuItem4 and menuItem5.
15 16 | menuItem4 = new Command("Item 4", Command.SCREEN, 5); menuItem5 = new Command("Item 5", Command.SCREEN, 6); |
If we were to change lines 12 to 15, so that menuItem1, menuItem4 and menuItem5 now have the same priority (level 2),
12 13 14 15 16 | menuItem1 = new Command("Item 1", Command.SCREEN, 2); menuItem2 = new Command("Item 2", Command.SCREEN, 3); menuItem3 = new Command("Item 3", Command.SCREEN, 4); menuItem4 = new Command("Item 4", Command.SCREEN, 2); menuItem5 = new Command("Item 5", Command.SCREEN, 2); |
then we get the menu items appearing similar to the screenshot below.

Figure 2 : Midlet Screen With Multiple Menu Items (Equal Priority Levels)
Here, the menu items,
menuItem1, menuItem4 and menuItem5 (which have equal priority levels) appeared in the order in which they were added to the Form.