Create J2ME MidLet with Command, List and Alert

The following is a simple example of creating a Command, List and the Alert in J2ME Midlet. The first thing you should do is install the JDK on the C system, then install the Java ™ Platform Micro Edition Software Development Kit or Netbean built J2ME.

This program that we will create:

list menu :


select one list :


result alert :





The following step program creation :


Step #1 : create all object needed and declare it


public class Menu21 extends MIDlet implements CommandListener{
//create object needed
private List mainMenu;
private List titleFilm;
private List contactkAdm;
private List scheduleFilm;
private Command cmdExit;
private Command cmdBack;
private Command cmdSelect;
private Display display;
private Alert alert1;
private Image img = null;

public Menu21(){
//declare object
display = Display.getDisplay(this);
mainMenu = new List("Theatre21", Choice.IMPLICIT);
titleFilm = new List("Title", Choice.IMPLICIT);
contactkAdm = new List("Contact", Choice.EXCLUSIVE);
scheduleFilm = new List("Schedule", Choice.MULTIPLE);
cmdBack = new Command("Back", Command.BACK, 1);
cmdExit = new Command("Exit", Command.EXIT, 2);
cmdSelect = new Command("Select", Command.SCREEN, 2);
alert1 = new Alert(null);
alert1.setTimeout(10000);

}



Step #2: makes all the objects and fill in the required content in objects startApp ()


public void startApp() {
//create object icon
try {
img = Image.createImage("/small.png");
}catch(Exception e){
System.out.println("image null " +e.getMessage());
img=null;
}

//add command to main menu
mainMenu.addCommand(cmdExit);
mainMenu.addCommand(cmdPilihl);

//add list to main menu
mainMenu.append("Title", img);
mainMenu.append("contact", img);
mainMenu.append("Schedule", img);
mainMenu.append("Info", img);

//set command main menu
mainMenu.setCommandListener(this);
//set main menu as displayable
display.setCurrent(mainMenu);

}


Step #3 : Create Command listener


public void commandAction(Command c, Displayable d) {
if (c==cmdExit){
destroyApp(false);
notifyDestroyed();
}else if(c==cmdPilihl){
if(display.getCurrent()==mainMenu){
evenMainMenu();
}else if(display.getCurrent()==titleFilm){
evenTitle();
}else if(display.getCurrent()==contactkAdm){
evenContact();
}else if(display.getCurrent()==scheduleFilm){
evenSchedule();
}
}else if(c==cmdBack){
display.setCurrent(mainMenu);
}
}




Step #4 : Create all Object needed in action listener

private void evenJudul() {
int i = titleFilm.getSelectedIndex();
alert1.setTitle("Title " + titleFilm.getString(i));
if(titleFilm.getString(i).equals("Avatar")){
alert1.setString("i think you know about this film...");
}else if(titleFilm.getSelectedIndex()==1){
alert1.setString("menceritakan tentang bumi th 2012");
}else if(titleFilm.getSelectedIndex()==2){
alert1.setString("i don't know about this film..");
}else if(titleFilm.getSelectedIndex()==3){
alert1.setString("story about Gi Joe the 'Bolang'");
}
display.setCurrent(alert1);
}


private void evenContact() {
int i = contactkAdm.getSelectedIndex();
alert1.setTitle(contactkAdm.getString(i)+" kami :\n");
if(i==0){
alert1.setString("cieforblack@gmail.com");
}else if(i==1){
alert1.setString("cie_78@yahoo.com");
}else if(i==2){
alert1.setString("ciefor78@gmail.com");
}
display.setCurrent(alert1);
}

private void evenSchedule() {
boolean[] selctSchedule = new boolean[scheduleFilm.size()];
int jumpil = scheduleFilm.getSelectedFlags(selectSchedule);
alert1.setTitle("Schedule 21");

StringBuffer isi = new StringBuffer("You select "+jumpil+" hari\n");
for(int i=0;i if(selectSchedule[i])
isi.append((i+1)+". hari "+scheduleFilm.getString(i)+"\n");
}

alert1.setString(isi.toString());
display.setCurrent(alert1);
}

private void evenMainMenu() {
if(mainMenu.getSelectedIndex()==0){
listtitle();
}else if(mainMenu.getSelectedIndex()==1){
listContact();
}else if(mainMenu.getSelectedIndex()==2){
listSchedule();
}else if(mainMenu.getSelectedIndex()==3){
info21();
}
}


private void info21() {
alert1.setTitle("INFORMATION");
alert1.setString("Bioskop21 Information\n"
+"Name : Widya\n"
+"Nim : 07.11.1634");

display.setCurrent(alert1);
}

private void listSchedule() {
jadwalFilm.deleteAll();

scheduleFilm.addCommand(cmdExit);
scheduleFilm.addCommand(cmdBack);
scheduleFilm.addCommand(cmdPilihl);

scheduleFilm.append("Monday", null);
scheduleFilm.append("Tuesday", null);
scheduleFilm.append("Wednesday", null);
scheduleFilm.append("Thursday", null);
scheduleFilm.append("Friday", null);
scheduleFilm.append("Saturday", null);
scheduleFilm.append("Sunday", null);

scheduleFilm.setCommandListener(this);
display.setCurrent(scheduleFilm);
}

private void listTitle() {
try {
img = Image.createImage("/small.png");
}catch(Exception e){
System.out.println("image null " +e.getMessage());
img=null;
}

titleFilm.deleteAll();

titleFilm.addCommand(cmdExit);
titleFilm.addCommand(cmdPilihl);
titleFilm.addCommand(cmdBack);

titleFilm.append("Avatar", img);
titleFilm.append("2012", null);
titleFilm.append("UP IN THE AIR", null);
titleFilm.append("GI JOE", null);

titleFilm.setCommandListener(this);
display.setCurrent(judulFilm);
}

private void listContact() {

contactAdm.deleteAll();
contactAdm.addCommand(cmdExit);
contactAdm.addCommand(cmdBack);
contactAdm.addCommand(cmdSelect);

contactAdm.append("E-Mail", null);
contactAdm.append("Facebook", null);
contactAdm.append("Yahoo", null);

contactAdm.setCommandListener(this);
display.setCurrent(contactAdm);
}