Add or remove controls from a form : Form « J2ME « Java Tutorial






import java.io.IOException;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.Ticker;
import javax.microedition.midlet.MIDlet;

public class MIDPApplication extends MIDlet implements CommandListener {
  Ticker newsTicker = new Ticker("Java J2ME");

  private List menuList = new List(null, List.IMPLICIT, menuItem, menuImage);

  private List luckyList;

  private List commList;

  private Command exitCommand = new Command("Exit", Command.EXIT, 1);

  private Command execCommand = new Command("Exe", Command.OK, 1);

  private Command menuCommand = new Command("Main", Command.SCREEN, 1);

  private Command commCommand = new Command("Contact", Command.SCREEN, 1);

  private Command luckyCommand = new Command("Plan", Command.SCREEN, 1);

  private Command saveCommand;

  String[] menuItem = { "Contact", "Plan" };

  String[] commItem = { "Add", "Search", "Edit", "Delete" };

  String[] luckyItem = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L" };

  Image[] menuImage = { createImage("/S.png"), createImage("/T.png") };

  private Display display = Display.getDisplay(this);

  private String currentScreen = "";

  public void startApp() {
    menuList.setTicker(newsTicker);
    menuList.addCommand(exitCommand);
    menuList.setCommandListener(this);
    display.setCurrent(menuList);
    currentScreen = "Main";

  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }

  public void commandAction(Command c, Displayable s) {
    if (c == exitCommand) {
      destroyApp(false);
      notifyDestroyed();
    }

    if (c == List.SELECT_COMMAND) {
      if (currentScreen == "Main") {
        int itemIndex = menuList.getSelectedIndex();
        switch (itemIndex) {
        case 0: {
          doCommunication();
          break;
        }
        case 1:
          doLucky();
        }
      } else if (currentScreen == "Contact") {
        int itemIndex = commList.getSelectedIndex();
        switch (itemIndex) {
        case 0: {
          doAppend();
          break;
        }
        case 1: {
          doQuery();
          break;
        }
        case 2: {
          doModification();
          break;
        }
        case 3: {
          doDelete();
          break;
        }
        }

      } else {

      }
    }

    if (c == menuCommand) {
      display.setCurrent(menuList);
      currentScreen = "Main";
    }

    if (c == commCommand) {
      doCommunication();

    }

    if (c == luckyCommand) {
      doLucky();
    }

    if (c == menuCommand) {
      doMenu();

    }

    if (c == execCommand) {
      if (currentScreen == "Plan") {
        showLucky();
      }
    }
  }

  private Image createImage(String name) {
    Image aImage = null;
    try {
      aImage = Image.createImage(name);
    } catch (IOException e) {
    }
    return aImage;
  }

  private void doMenu() {
    currentScreen = "Main";
    display.setCurrent(menuList);
  }

  private void doCommunication() {
    Image[] commIcon = { createImage("/S.png"), createImage("/Sk.png"),
        createImage("/M.png"), createImage("/T.png") };

    commList = new List("Contact", List.IMPLICIT, commItem, commIcon);

    commList.addCommand(luckyCommand);
    commList.addCommand(menuCommand);
    commList.setCommandListener(this);
    currentScreen = "Contact";
    display.setCurrent(commList);
  }

  private void doLucky() {
    luckyList = new List("Plan", List.EXCLUSIVE, luckyItem, null);

    luckyList.addCommand(commCommand);

    luckyList.addCommand(menuCommand);
    luckyList.addCommand(execCommand);
    luckyList.setCommandListener(this);
    currentScreen = "Plan";
    display.setCurrent(luckyList);
  }

  private void showLucky() {
    int selectedItem = luckyList.getSelectedIndex();
    Alert alert;
    String information = "";
    switch (selectedItem) {
    case 0: {
      information = "A";
      break;
    }
    case 1: {
      information = "B";
      break;
    }
    case 2: {
      information = "C";
      break;
    }
    case 3: {
      information = "D";
      break;
    }
    case 4: {
      information = "E";
      break;
    }
    case 5: {
      information = "F";
      break;
    }
    case 6: {
      information = "G";
      break;
    }
    case 7: {
      information = "H";
      break;
    }
    case 8: {
      information = "I";
      break;
    }
    case 9: {
      information = "J";
      break;
    }
    case 10: {
      information = "K";
      break;
    }
    case 11: {
      information = "L";
      break;
    }
    }

    alert = new Alert("Info", information, null, AlertType.INFO);
    alert.setTimeout(Alert.FOREVER);
    display.setCurrent(alert);
  }

  private void doAppend() {
    Form appendForm = new Form("Add");
    saveCommand = new Command("Save", Command.SCREEN, 1);
    TextField nameField = new TextField("Name", null, 10, TextField.ANY);
    TextField EMailField = new TextField("E Mail", null, 10, TextField.EMAILADDR);
    TextField ageField = new TextField("Age", null, 10, TextField.NUMERIC);
    appendForm.append(nameField);
    appendForm.append(EMailField);
    appendForm.append(ageField);
    appendForm.addCommand(saveCommand);
    appendForm.addCommand(commCommand);
    appendForm.addCommand(luckyCommand);
    appendForm.setCommandListener(this);
    currentScreen = "ContactAdd";
    display.setCurrent(appendForm);
  }

  private void doModification() {
    Form modificationForm = new Form("Edit");
    TextField nameField = new TextField("Name", null, 10, TextField.ANY);
    modificationForm.append(nameField);
    modificationForm.addCommand(execCommand);
    modificationForm.addCommand(commCommand);
    modificationForm.addCommand(luckyCommand);
    modificationForm.setCommandListener(this);
    currentScreen = "ContactEdit";
    display.setCurrent(modificationForm);
  }

  private void doQuery() {
    Form queryForm = new Form("Search");
    TextField nameField = new TextField("Name", null, 10, TextField.ANY);
    queryForm.append(nameField);
    queryForm.addCommand(execCommand);
    queryForm.addCommand(commCommand);
    queryForm.addCommand(luckyCommand);
    queryForm.setCommandListener(this);
    currentScreen = "ContactSearch";
    display.setCurrent(queryForm);
  }

  private void doDelete() {
    Form deleteForm = new Form("Delete");
    TextField nameField = new TextField("Name", null, 10, TextField.ANY);
    deleteForm.append(nameField);
    deleteForm.addCommand(execCommand);
    deleteForm.addCommand(commCommand);
    deleteForm.addCommand(luckyCommand);
    deleteForm.setCommandListener(this);
    currentScreen = "ContactDelete";
    display.setCurrent(deleteForm);
  }
}








31.3.Form
31.3.1.Form demoForm demo
31.3.2.extends Form to create a new formextends Form to create a new form
31.3.3.Add controls to formAdd controls to form
31.3.4.GUI testGUI test
31.3.5.Add or remove controls from a form
31.3.6.creates a Form with two items, an interactive Gauge and a StringItemcreates a Form with two items, an interactive Gauge and a StringItem