Command dan Alert di J2ME

Posted in: Knowledge by krisnarengga on June 1, 2009


Command Class merepresentasikan aksi terhadap suatu informasi yang ditampilkan. Command identik dengan button pada pemrograman aplikasi di platform yang lain. Behavior atau aksi yang akan dilakukan apabila command di invoke tidak didefinisikan pada method dari objek ini, tetapi akan didefinisikan pada method commandAction pada interface CommandListener. Class Command memiliki beberapa tipe antara lain BACK, CANCEL, EXIT, HELP, ITEM, OK, SCREEN dan STOP. Command Class juga memiliki tingkat prioritas yang menunjukkan hierarki dari objek command. Berikut merupakan listing program pengimplementasian objek dari Class Command dan objek dari Class Alert dengan tipe alert alarm, alert confirmation, alert error, alert info dan alert warning.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* @author Krisna Rengga Buana
*/
public class MyCommand extends MIDlet implements CommandListener {
private Display display = null;
private Form form = null;
private Command exit = null;
private Command [] item = null;
private Alert alert = null;
public MyCommand()
{
form = new Form(“My Command”);
exit = new Command(“Exit”,Command.EXIT,1);
item = new Command[5];
for(int i=0;i<item.length;i++)
{
item[i] = new Command(“Item “+(i+1),Command.ITEM,1);
form.addCommand(item[i]);
}
form.addCommand(exit);
form.setCommandListener(this);
}
public void startApp() {
if(display == null)
{
display = Display.getDisplay(this);
display.setCurrent(form);
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c,Displayable d)
{
if(c == exit)
{
notifyDestroyed();
destroyApp(true);
}
else if(c == item[0])
{
alert = new Alert(“Alarm”,”Ini command item 1, Tipe Alert Alarm”,null,AlertType.ALARM);
display.setCurrent(alert);
}
else if(c == item[1])
{
alert = new Alert(“Confirmation”,”Ini command item 2, Tipe Alert Confirmation”,null,AlertType.CONFIRMATION);
display.setCurrent(alert);
}
else if(c == item[2])
{
alert = new Alert(“Error”,”Ini command item 3, Tipe Alert Error”,null,AlertType.ERROR);
display.setCurrent(alert);
}
else if(c == item[3])
{
alert = new Alert(“Information”,”Ini command item 4, Tipe Alert Info”,null,AlertType.INFO);
display.setCurrent(alert);
}
else if(c == item[4])
{
alert = new Alert(“Warning”,”Ini command item 5, Tipe Alert Warning”,null,AlertType.WARNING);
display.setCurrent(alert);
}
}
}
ini link source code nya 😛
http://www.kitaupload.com/download.php?file=16MyCommand.rar

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment