10 Oct

Tutorial Java ME – Part 4 Command

Macam-macam Command pada Java ME (J2ME):
BACK = navigasi ke layar sebelumnya
CANCEL = jawaban negatif dari dialog
EXIT = keluar aplikasi
HELP = permintaan menampilkan help
ITEM = aksi pemilihan item tertentu
OK = jawaban positif dari dialog
SCREEN = command pada screen tertentu
STOP = menghentikan proses yang berjalan

Contoh Penggunaan Command pada Java ME (J2ME):

public class TesGUI extends MIDlet implements CommandListener {
    private Display display;
    private Command cmdExit;

    public void startApp() {
        display = Display.getDisplay(this);
        
        Form f = new Form("Judul Form");
        cmdExit = new Command("Exit", Command.EXIT, 1);
        f.addCommand(cmdExit);
        f.setCommandListener(this);
        
        display.setCurrent(f);
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
        if (c == cmdExit) {
            destroyApp(true);
            notifyDestroyed();
        }
    }
}

Contoh Penggunaan CommandPriority pada Java ME (J2ME):

	private Command cmdExit,cmdOK,cmdBack,cmdCancel;

    public void startApp() {
        display = Display.getDisplay(this);
        
        Form f = new Form("Judul Form");
        cmdExit = new Command("Exit", Command.EXIT, 0);
        cmdOK = new Command("OK", Command.OK, 0);
        cmdBack = new Command("Back", Command.BACK, 0);
      cmdCancel = new Command("Cancel",Command.CANCEL, 0);

        f.addCommand(cmdExit);
        f.addCommand(cmdOK);
        f.addCommand(cmdBack);
        f.addCommand(cmdCancel);
        f.setCommandListener(this);
        
        display.setCurrent(f);
    }