Creating Commands: Command()

The first argument (a String "Exit") is the label of the command. This is what the user will see.

Command.Exit is the type of command. It tells the program that the user wants to exit the program.
Commands can be of eight types BACK, CANCEL, EXIT, HELP, ITEM, OK, SCREEN, and STOP. The names are self-explanatory except ITEM that corresponds to a specific item e.g. a choice made in a list and SCREEN that specifies an application-defined command that pertains to the current screen.

The int passed as the third argument defines the priority for the command, the lower the value the more priority it gets. The priority values are used to determine display positions of the commands. However how exactly they are displayed is device-dependent.
Say there are five commands exit, open, save, search and clear with decreasing priority level (i.e. increasing priority value). A device which has place for only two commands will show the exit command and bunch all others in 'options' extending which will allow the user from choosing the rest of the options. A device with three places might show exit, open and club the other three under 'options'.

Displaying commands: addCommand()

Just creating the commands do not make them visible to the user. You need to add the commands to a displayable object e.g. a Form.

Say we have a Form named mainForm and we want to add the exitCmd to that form. We can do that by -
mainForm.addCommand(exitCmd);
You can add as many commands you like.

Note a List has a special select command, which determines what happens when the user selects an element in the list. Suppose we present the user with imageList, a list of image names and we want to open the image when the user selects it. For that we first create a command names openCmd and set it as the select command -
imageList.setSelectCommand(openCmd);

There can be only one select command. If you don't set a select command generally the device will still give a 'select' option for the user. But it won't be of any use.

The commands can be added to any displayable unit. Whenever the displayable unit is shown, the commands are displayed with it.

Commands can be removed using removeCommand() method of a displayable unit.

Working with commands: CommandListener

To take action based on the commands a user clicks, you need a class to listen to the commands. For this purpose a class needs to implement the CommandListener class. It has to have a function commandAction(Command c, Displayable d).

Say we have our MIDlet implement the CommandListener and our mainForm has a number of commands. First we need to set the MIDlet as the listener for the commands in mainForm by
mainForm.setCommandListener(midlet);

Once this is done whenever a user clicks on any command, say exitCmd, on mainForm, commandAction() in MIDlet is called where mainForm is passed as the diaplayable and exitCmd is passed as the command.

One class can be the command listener for a number of displayable objects. It is the responsibility of the commandAction() function to determine where and which command was clicked by the user and take action accordingly.