provided by: 
Originally published at Internet.comJava Programming Notes # 2586 * Preface * Viewing tip * Figures * Listings * Supplementary material * General background information * Preview * Discussion and sample code * The Command class * The MIDlet named Command02 * The CommandListener interface * The MIDlet named Command03 * The MIDlet named Command04 * The ItemCommandListener interface * The MIDlet named Command05 * Run the programs * Summary * What's next? * Resources * Complete program listings * Copyright * About the author -----------------------------------
Preface
This is one in a series of tutorial lessons designed to teach you how to write programs using the Sun Java Wireless Toolkit for CLDC. The first lesson was titled Getting Started with MIDlets and the Sun Java Wireless Toolkit for CLDC. The previous lesson was titled Using Forms and Items in MIDlets (see Resources).
What you will learn
I will begin by illustrating and explaining the addition of Command objects to a MIDlet and showing you how those commands are presented to the user in the Sun cell phone emulator.
Interactive behavior of a MIDlet is achieved, not through commands, but rather through the use of a CommandListener object or an ItemCommandListener object. I will illustrate and explain the setting and subsequent processing of a CommandListener in one of the simplest ways possible.
Following that, I will present and explain a much more substantive MIDlet. This MIDlet illustrates the use of CommandListener objects to handle seven of the eight possible types of commands and shows how to use commands to navigate through the different pages of a MIDlet that is composed of several pages.
Finally, I will illustrate and explain the use of an ItemCommandListener to handle an ITEM command on an item contained in a Form.
Viewing tip
I recommend that you open another copy of this document in a separate browser window and use the following links to easily find and view the figures and listings while you are reading about them.
Figures
* Figure 1. Partial class hierarchy for MIDP 2.0. * Figure 2. Sun cell phone emulator with nine commands showing. * Figure 3. The commandAction method. * Figure 4. The MIDlet named Command03 in the Sun emulator. * Figure 5. Sun emulator screen when the MIDlet is not running. * Figure 6. General behavior of each command type. * Figure 7. Screen shot of Command04 in the Sun cell phone emulator. * Figure 8. ChoiceGroup selected in Sun cell phone emulator. * Figure 9. Result of activating the GetSelection command.
Listings
* Listing 1. Beginning of MIDlet class named Command02. * Listing 2. Add four commands to the TextBox. * Listing 3. Add four more commands to the TextBox. * Listing 4. Remaining code in the MIDlet named Command02. * Listing 5. Beginning of the class for the MIDlet named Commond03. * Listing 6. Add two commands to the TextBox. * Listing 7. Set a commandListener object on the TextBox. * Listing 8. Remaining code for the MIDlet named Command03. * Listing 9. Beginning of the class for the MIDlet named Command04. * Listing 10. Set a CommandListener on the page 1 TextBox. * Listing 11. Remaining code for the MIDlet named Command04. * Listing 12. Beginning of the class for the MIDlet named Command05. * Listing 13. Append a StringItem and an ImageItem to the Form. * Listing 14. Append a ChoiceGroup object to the Form. * Listing 15. Add an ITEM command and an ItemCommandListener to the ChoiceGroup object. * Listing 16. Source code for the MIDlet named Command02. * Listing 17. Source code for the MIDlet named Command03. * Listing 18. Source code for the MIDlet named Command04. * Listing 19. Source code for the MIDlet named Command05.
Supplementary material
I recommend that you also study the other lessons in my extensive collection of online Java tutorials. You will find a consolidated index at www.DickBaldwin.com.
General background information
If you were expecting the interactive programming capability of J2ME to be similar to the JavaBeans or Delegation Event models in J2SE (see Resources), you are in for a big disappointment. The model is more akin to the somewhat rudimentary models that I describe in the following tutorial lessons. * Event Handling in JDK 1.0.2, The Event Class (see Resources) * Handling Events in Extended Components without Listener Objects (see Resources)
Partial class hierarchy for MIDP 2.0
A partial class hierarchy for MIDP 2.0 is shown in Figure 1.
Figure 1. Partial class hierarchy for MIDP 2.0. * Object * Displayable * Screen * TextBox * Alert * List * Form * Canvas (abstract) * GameCanvas (abstract) * Display * Ticker * AlertType * Image * Item * Gauge * ChoiceGroup * CustomItem
Author: Richard G. Baldwin
Read article at Internet.com site