Command dan Alert di J2ME


 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😛

a simple man

Posted in Knowledge

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

February 2009
M T W T F S S
« Dec   Mar »
 1
2345678
9101112131415
16171819202122
232425262728  
Blog Stats
  • 72,745 hits
%d bloggers like this: