Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile

J2ME: Using StringItems and Commands

5.00/5 (3 votes)
17 Apr 2011CC (ASA 3U)5 min read 52.6K   1.4K  
This is a J2ME beginner article that describes how to add different J2ME components to your application especially StringItems and Commands.

Introduction

After creating your application's main interface, the next thing you should think of is to add components to the interface, and allow the user to execute commands. Continue reading to learn how to add new items and commands to your Displayable, how to create and update StringItem content to display uneditable text and how to create various Commands and handle Command actions.

In this article, you will create the "Say Hello" application. The application contains a main form with a string item that has a label and some text. The main form also has two commands: Say Hello and Exit commands. The Say Hello command when selected by the user changes the string item label and text, but the Exit command will be used to exit the application and destroy all objects.

Note: You may be interested in reading the previous J2ME article as well: J2ME: Hello World Mobile Application.

Following is a screenshot of how the final application will look like:

say-hello-screeshot.png

Figure: Application screenshot

Creating Components

The first step in building the "Say Hello" application is to create the components, the string item and the two commands. The string item is one of the items you can add to a Form displayable. String items are used to display uneditable text and each consists of two parts: the label and text, and to use a string item you first need to create an object of type StringItem class and call the constructor. The constructor takes two string parameters for the label and text. Commands are different kinds of components that are used for user interaction by allowing the user to execute some functionality. When creating a command, you should specify three things:

  • The label as a string that is shown to represent this command.
  • The type to specify the intent of this command which can be used by the device when placing it on display.
  • And the priority that is used to describe the importance of this command relative to other commands on the same screen, with smaller numbers means higher priority.

For example, an Exit command will have "Exit" as a label, Exit type and may have the lowest priority relative to other commands.

Start by adding three data members to your MIDlet class, the first of type StringItem which is used to display the text and the other two of type Command for the Say Hello and Exit commands.

Java
public class SayHelloMIDlet extends MIDlet {
    ...
    private StringItem helloString;
 
    private Command sayHelloCmd;
    private Command exitCmd;
    ...
}

In the constructor of your MIDlet class, first create an instance of StringItem class and pass two parameters to its constructor, the label and text strings.

Java
public SayHelloMIDlet() {
    ...
    helloString = new StringItem("Some Label:", "Some Text");
}

In the same constructor and following the StringItem object creation, create two instances of Command class for each of the two commands. In Command’s constructor pass the label, type and priority parameters. You will choose the SCREEN type for the Say Hello command and EXIT type for the Exit command with the Say Hello having a higher priority which is zero.

Java
public SayHelloMIDlet() {
    ...
    sayHelloCmd = new Command("Say Hello", Command.SCREEN, 0);
    exitCmd = new Command("Exit", Command.EXIT, 1);
}

Adding Components to Form

The next step after creating any item or Command is to add them to your Form to be displayed (or any other Displayable object in case of Command components). There are two ways to add an item to a Form, using the constructor or using the Form class append() method. In this application, you will use the append() method. Commands are added to a Displayable using the addCommand() method.

So, you should add the StringItem to helloFrm by using the append() method.

Java
public SayHelloMIDlet() {
    ...
    helloFrm.append(helloString);
    ...
}

Next, you add the two commands using the addCommand() method.

Java
public SayHelloMIDlet() {
    ...
    helloFrm.addCommand(sayHelloCmd);
    helloFrm.addCommand(exitCmd);
    ...
}

Handling Command Actions

If you tried to run the application, until this point it will work as required except for the two commands they will do nothing, because we did not specify the command action yet. The command action is the code to be executed when the user selects that command. The command action can be handled by first implementing the CommandListener interface and then set the command listener using the setCommandListener() method to that implementation.

First, implement the CommandListener interface and its commandAction() method in your MIDlet class.

Java
public class SayHelloMIDlet extends MIDlet implements CommandListener {
    ...
    public void commandAction(Command c, Displayable d) {
    }
}

Now, set the command listener of your Form to your MIDlet class, by calling the setCommandListener() method in your MIDlet constructor and passing a reference to the MIDlet object.

Java
public SayHelloMIDlet() {
    ...
    helloFrm.setCommandListener(this);
} 

Now, let’s add the specific action for each command in the commandAction() method, and to do that, you can use the two parameters of the method. The first parameter specifies the command that was selected in case the application has more than one command as in this case. The second specifies the Displayable object that contains the command in case you added the same command to more than one Displayable.

You can simply use if/else statement to check the displayable and the specific command.

Java
public void commandAction(Command c, Displayable d) {
    if (d == helloFrm) {
        if (c == sayHelloCmd) {
            //Say Hello command action
        } else if (c == exitCmd) {
            //Exit command action
        }
    }
}

The command action for the Say Hello command is to change the label and text of the string item. You can change the string item label and text by calling setLabel() and setText() methods of the StringItem object.

Java
if (c == sayHelloCmd) {
    helloString.setLabel("Hello");
    helloString.setText("J2ME");
}

The Exit command action will be used to exit the application and destroy all objects, and to do that you simply call the notifyDestroyed() method.

Java
if (c == exitCmd) {
    notifyDestroyed();
}

Conclusion

You learned in this article how to use the string item and command J2ME components. You also learned how to add different components to the displayable and how to handle the command actions by implementing the CommandListener interface and commandAction() method.

Following is the complete application code:

Java
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
 
public class SayHelloMIDlet extends MIDlet implements CommandListener {
    private Display display;
    private Form helloFrm;
    private StringItem helloString;
 
    private Command sayHelloCmd;
    private Command exitCmd;
 
    public SayHelloMIDlet() {
        helloFrm = new Form("Hello J2ME");
 
        helloString = new StringItem("Some Label:", "Some Text");
 
        sayHelloCmd = new Command("Say Hello", Command.SCREEN, 0);
        exitCmd = new Command("Exit", Command.EXIT, 1);
 
        helloFrm.append(helloString);
 
        helloFrm.addCommand(sayHelloCmd);
        helloFrm.addCommand(exitCmd);
 
        helloFrm.setCommandListener(this);
    }
 
    protected void startApp() {
        display = Display.getDisplay(this);
        display.setCurrent(helloFrm);
    }
 
    public void commandAction(Command c, Displayable d) {
        if (d == helloFrm) {
            if (c == sayHelloCmd) {
                helloString.setLabel("Hello");
                helloString.setText("J2ME");
            } else if (c == exitCmd) {
                notifyDestroyed();
            }
        }
    }
 
    protected void pauseApp() {
    }
 
    protected void destroyApp(boolean unconditional) {
    }
}

History

  • 17th April, 2011: Initial post

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-Share Alike 3.0 Unported License