Get Label and text from StringItem : StringItem « J2ME « Java Tutorial






Get Label and text from StringItem
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.StringItem;
import javax.microedition.midlet.MIDlet;

public class StringItemMIDlet extends MIDlet implements CommandListener {

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

  private Command setLabelCommand = new Command("Label", Command.SCREEN, 1);

  private Command setTextCommand = new Command("Text", Command.SCREEN, 1);

  private Command getLabelTextCommand = new Command("Get", Command.SCREEN, 1);

  private StringItem stringItem1 = new StringItem("Label1", "Content1");

  private StringItem stringItem2 = new StringItem(null, "Content2");

  private Display display = Display.getDisplay(this);

  public StringItemMIDlet() {
  }

  public void startApp() {
    Form aForm = new Form("StringItem");

    aForm.append(stringItem1);
    aForm.append(stringItem2);
    aForm.addCommand(exitCommand);
    aForm.addCommand(setLabelCommand);
    aForm.addCommand(setTextCommand);
    aForm.addCommand(getLabelTextCommand);
    aForm.setCommandListener(this);
    display.setCurrent(aForm);
  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }

  public void commandAction(Command c, Displayable s) {
    if (c == exitCommand) {
      destroyApp(false);
      notifyDestroyed();
    }
    if (c == setLabelCommand)
      stringItem1.setLabel("new label");
    if (c == setTextCommand)
      stringItem2.setText("new text");
    if (c == getLabelTextCommand) {
      Alert alert = new Alert("Info", stringItem1.getLabel() + ":" + stringItem1.getText(), null,
          AlertType.INFO);
      alert.setTimeout(5000);
      display.setCurrent(alert);
    }
  }
}








31.4.StringItem
31.4.1.Add StringItem to a formAdd StringItem to a form
31.4.2.StringItem with line breakStringItem with line break
31.4.3.Get Label and text from StringItemGet Label and text from StringItem
31.4.4.Create Form with a list of StringItemsCreate Form with a list of StringItems
31.4.5.Set new text value to StringItemSet new text value to StringItem