J2ME tutorial-Command



Command is a useful item in every application. It tells the program to perform a specific action when the user choose to do so. In other words, the command increases the users' interactivity with your software. A command object can be created by using the following prototype:

Command commadObj=new Command(Label,Type,Priority);

Label is the text to display on the command button. The Type argument specifies the type of command. The values of the Type argument can be Command.BACK, Command.CANCEL, Command.EXIT, Command.HELP, Command.ITEM, Command.OK, Command.SREEN, and Command.STOP. The Priority sets the priority of the command. If the value is 0, it is the highest priority command. The higher the value the lower the priority is.

The command can be attached to the form by using addCommand method of the form. To enable the form to listen to the actions that will perform by the user, you need to set the CommandListener interface to the form by using setCommandListener method.

Form form=new Form("Form");



The this keyword refers to the current CommandListener interface that the class is currently implemented. This interface has a special method named commandAction. In this method you can write code to response to the command action. In the example code below, when the user clicks on Cancel command the program will exit and the memory use is released. By clicking on the Ok command the program will call the check method and alert whether the user id and password are correct or incorrect.


import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class Login extends MIDlet implements CommandListener{
private Display display;
private Form form;
private TextField txtname,txtpassword;
private Command Cmdok, Cmdcancel;

public Login() {
display = Display.getDisplay(this);


public void startApp() {
form=new Form("Login");
txtname=new TextField("User id:","",250,TextField.ANY);
txtpassword=new TextField("Password:","",250,TextField.PASSWORD);
Cmdok=new Command("Ok",Command.OK, 0);
Cmdcancel=new Command("Cancel",Command.CANCEL,0);



public void pauseApp() {}

public void destroyApp(boolean unconditional) {}

public void commandAction(Command c, Displayable s) {
if(c==Cmdcancel) notifyDestroyed();
else if(c==Cmdok) check();

public void check(){
if(txtname.getString().equals("dara") && txtpassword.getString().equals("123")){
Alert al=new Alert("Correct","Welcome!",null, AlertType.INFO);
Alert al=new Alert("Incorrect","Invalid login!",null, AlertType.ERROR);

Note: to read the value of a text field you can use getString() method of the text field object. If you want to write text programmatically to the text field you need to use setString(text value) method.



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.