Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home J2me J2ME Command Class

Related Tutorials


 
 

Share on Google+Share on Google+

J2ME Command Class

Advertisement
In the given J2ME Command Class example, we have set the various commands namely, screen, back, cancel, ok, help, stop, exit and item to the Midlet.

J2ME Command Class

     

In the given J2ME Command Class example, we have set the various commands namely, screen, back, cancel, ok, help, stop, exit and item to the Midlet. And also set the priority for it such as 1, 2, 3 etc. In J2ME commands are used for keeping information of the commands, not the action. For defining actions, CommandListener class are used. In this example we are going to show different commands on the screen but we have not used CommandListner class into it, that's these commands will not perform any action.

 

 

 

 

 

Our command class Midlet will look like as follow..

J2ME Command Class

Source Code of CommandMIDlet.java

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

public class CommandMIDlet extends MIDlet{
  private Form form;
  private Display display;
  private Command screen, back, cancel, ok, help, stop, exit, item;  
  
  public CommandMIDlet(){
  screen = new Command("Screen", Command.SCREEN, 1);
  back = new Command("Back", Command.BACK, 2);
  cancel = new Command("Cancel", Command.CANCEL, 3);
  ok = new Command("Ok", Command.OK, 4);
  help = new Command("Help", Command.HELP, 5);
  stop = new Command("Stop", Command.STOP, 6);
  exit = new Command("Exit", Command.EXIT, 7);
  item = new Command("Item", Command.ITEM, 8);
  }

  public void startApp(){
  display = Display.getDisplay(this);
  Form form = new Form("Command Priority");
  form.addCommand(screen);
  form.addCommand(back);
  form.addCommand(cancel);
  form.addCommand(ok);
  form.addCommand(help);
  form.addCommand(stop);
  form.addCommand(exit);
  form.addCommand(item);
  display.setCurrent(form);
  }

  public void pauseApp(){}

  public void destroyApp(boolean destroy){
  notifyDestroyed();
  }
}

 

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 20, 2008

Related Tutorials

Discuss: J2ME Command Class   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
swetha
September 5, 2012
impend message to a command

how can i add some messages to the above commands
saiprasad
October 9, 2012
command class

how to enable & disable commands in j2me
Shani
June 6, 2014
Comment of Bug

hello dear in your post i Found a little bug, As you specified the priority of command your commands are not according that. means you have given the priority of Screen 1 but this is at position 2 and same for OK an Cancel. If you get any solution of this please share with us. Thanks Shani Goriwal
DMCA.com