List with strings and images : List « 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.Image;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;

public class ListMIDlet extends MIDlet implements CommandListener {
  TextBox main;

  private Command exitCommand;

  private Command exclusiveCommand;

  private Command multipleCommand;

  private Command implicitCommand;

  private Command confirmCommand;

  private List aEXCLUSIVEList;

  private List aMULTIPLEList;

  private List aIMPLICITList;

  String[] position = { "1", "2", "3" };

  String[] hobby = { "A", "B", "C", "D", "E" };

  Image[] positionIcon = { createImage("/1.png"), createImage("/2.png"), createImage("/3.png") };

  Image[] hobbyIcon = { createImage("/A.png"), createImage("/B.png"), createImage("/C.png"),
      createImage("/D.png"), createImage("/E.png") };

  private Display display;

  private String currentScreen = "";

  public ListMIDlet() {
    display = Display.getDisplay(this);
    exitCommand = new Command("exit", Command.SCREEN, 1);
    exclusiveCommand = new Command("exclusive", Command.SCREEN, 1);
    multipleCommand = new Command("multiple", Command.SCREEN, 1);
    implicitCommand = new Command("implicit", Command.SCREEN, 1);
    confirmCommand = new Command("confirm", Command.SCREEN, 2);

  }

  public void startApp() {
    main = new TextBox("main", null, 256, TextField.ANY);
    main.addCommand(exitCommand);

    main.addCommand(exclusiveCommand);
    main.addCommand(multipleCommand);
    main.addCommand(implicitCommand);

    main.setCommandListener(this);
    display.setCurrent(main);
    currentScreen = "Main";
  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }

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

  private void exclusiveList() {
    aEXCLUSIVEList = new List("exclusive", List.EXCLUSIVE, position, positionIcon);
    aEXCLUSIVEList.addCommand(exitCommand);
    aEXCLUSIVEList.addCommand(confirmCommand);
    aEXCLUSIVEList.setCommandListener(this);
    display.setCurrent(aEXCLUSIVEList);
    currentScreen = "exclusive";
  }

  private void multipleList() {
    aMULTIPLEList = new List("multiple", List.MULTIPLE, hobby, hobbyIcon);
    aMULTIPLEList.addCommand(exitCommand);
    aMULTIPLEList.addCommand(confirmCommand);
    aMULTIPLEList.setCommandListener(this);
    display.setCurrent(aMULTIPLEList);
    currentScreen = "multiple";
  }

  private void implicitList() {
    aIMPLICITList = new List("implicit", List.IMPLICIT);
    aIMPLICITList.append("A", createImage("/A.png"));
    aIMPLICITList.append("B", createImage("/B.png"));
    aIMPLICITList.append("C", createImage("/C.png"));
    aIMPLICITList.append("D", createImage("/D.png"));
    aIMPLICITList.append("E", createImage("/E.png"));
    aIMPLICITList.addCommand(exitCommand);

    aIMPLICITList.setCommandListener(this);
    display.setCurrent(aIMPLICITList);
    currentScreen = "implicit";
  }

  public void commandAction(Command c, Displayable s) {
    if (c == exitCommand) {
      if (currentScreen == "implicit") {
        destroyApp(false);
        notifyDestroyed();
      } else {
        display.setCurrent(main);
        currentScreen = "no";
      }
    }

    if (c == exclusiveCommand) {
      exclusiveList();
    }
    if (c == multipleCommand) {
      multipleList();
    }
    if (c == implicitCommand) {
      implicitList();
    }
    if (c == confirmCommand) {
      Alert aAlert;

      if (currentScreen == "implicit") {
        int aIndex = aEXCLUSIVEList.getSelectedIndex();
        aAlert = new Alert("Result", position[aIndex], null, AlertType.INFO);
        display.setCurrent(aAlert);
      }
      if (currentScreen == "multiple") {
        String result = "result\n";
        for (int i = 0; i < aMULTIPLEList.size(); i++)
          if (aMULTIPLEList.isSelected(i))
            result += hobby[i] + ", ";
        aAlert = new Alert("alert", result, null, AlertType.INFO);
        aAlert.setTimeout(5000);
        display.setCurrent(aAlert);
      }
    }
    if (c == List.SELECT_COMMAND) {
      int aIndex = aIMPLICITList.getSelectedIndex();
      String menuItem = aIMPLICITList.getString(aIndex);
      Alert aAlert = new Alert("imple", menuItem, null, AlertType.INFO);
      aAlert.setTimeout(5000);
      display.setCurrent(aAlert);
    }
  }

}








31.11.List
31.11.1.Use List
31.11.2.Use List option to hold choiceUse List option to hold choice
31.11.3.List with strings and images
31.11.4.Implicit MenuImplicit Menu
31.11.5.List check boxList check box
31.11.6.List radio buttonList radio button