Click here to Skip to main content
Click here to Skip to main content

J2ME: Using StringItems and Commands

, 17 Apr 2011 CC (ASA 3U)
Rate this:
Please Sign up or sign in to vote.
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.

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.

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.

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.

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

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

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.

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.

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.

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.

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.

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:

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

Share

About the Author

Fadi Hania
Software Developer (Senior) Wateen Technologies
Palestinian Territory (Occupied) Palestinian Territory (Occupied)
A professional developer with a B.Sc. in Computer Engineering. Developing Web, Moblie and Desktop applications and interested in reading and blogging. A cofounder of Wateen Technologies an application development and hosting company.
Visit Fadi Hania Blog to read articles written by Fadi Hania, follow Fadi Hania at Twitter and become a friend with Fadi Hania on Facebook
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 5 PinmemberAli Al Omairi(Abu AlHassan)17-Apr-11 1:33 
GeneralGood work again! PinmvpRichard MacCutchan16-Apr-11 3:18 
GeneralRe: Good work again! PinmemberFadi Hania16-Apr-11 3:51 
GeneralRe: Good work again! PinmvpRichard MacCutchan16-Apr-11 4:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 17 Apr 2011
Article Copyright 2011 by Fadi Hania
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid