10 Oct

Tutorial Java ME – Part 5 Alert

Contoh penggunaan Alert di Java ME (J2ME):

public class TesGUI extends MIDlet implements CommandListener {
    private Display display;
    private Command cmdExit,cmdOK;
    private Form f;

public void startApp() {
        display = Display.getDisplay(this);
        
     cmdExit = new Command("Exit", Command.EXIT, 0);
     cmdOK = new Command("OK", Command.OK, 0);
        
        f = new Form("Judul Form");
        f.addCommand(cmdExit);
        f.addCommand(cmdOK);        
        f.setCommandListener(this);
        
        display.setCurrent(f);
}

public void commandAction(Command c, Displayable d)
{
        if (c == cmdExit) {
            destroyApp(true);
            notifyDestroyed();
        } else if (c == cmdOK) {
            Alert a = new Alert(
                	"Judul", "Isi Alert", null, 
				AlertType.INFO);
            display.setCurrent(a, f);
        }
}
}

Type-type ALERT:
ALARM
CONFIRMATION
ERROR
INFO
WARNING

Cara membuat alert bersifat MODAL (tidak menutup jika tidak ditekan OK)

Alert a = new Alert("Judul", "Isi Alert", 
	null, AlertType.INFO);
a.setTimeout(Alert.FOREVER);