Adding Command Buttons

Description:

The following code shows how to add Command Buttons to a MIDlet.

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
public class Command_ex extends MIDlet implements CommandListener
{
    Display display=Display.getDisplay(this);
    Command ExitCmd = new Command ("Exit",Command.EXIT,7);
    Command CheckCmd = new Command ("Check",Command.SCREEN,1);
    public void startApp()
    {
        Form form = new Form("Commands example");
        form.append("This is a Command button example. Please press the Check button ");
        form.addCommand(ExitCmd);
        form.addCommand(CheckCmd);
        form.setCommandListener(this);
        display.setCurrent(form);
    }
    public void destroyApp(boolean unconditional)
    {
        notifyDestroyed();
    }
..........
    public void commandAction(Command c, Displayable d)
    {
        if (c==ExitCmd)
        {
            destroyApp(true);
        }
        else if (c==CheckCmd)
        {
            Alert alert = new Alert("Alert");
            alert.setString("Check button test");
            alert.setTimeout(3000); // 3 seconds
            display.setCurrent(alert, display.getCurrent() );
        }
    }
}

Download:

CommandButton.java

Output:

The following screenshots are the outcome. Just click on the icons to enlarge them for a better view.

Department of Electronic Systems | Aalborg University | Niels Jernes Vej 12 | DK-9220 Aalborg | Denmark
Phone: (+45) 9635 8695 | Fax (+45) 9815 1583 | E-mail ies(at)ies.aau.dk
Copyright © 2006 Department of Electronic Systems

Last update:11-10-2006