|   | Emerging Technology Fall Semester, 2004 MIDlet UI pt 2 | |
|---|---|---|
| © 2004, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 19-Oct-04 | 
CS 683 Emerging Technologies Fall Semester, 2004 Doc 20 MIDlet UI pt 2
Copyright ©, All rights reserved. 2004 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent ( http://www.opencontent.org/opl.shtml) license defines the copyright on this document.
| CS 683 Fall 04 | Doc 20, MIDlet UI pt 2 Slide # 2 | 
J2ME in a Nutshell, Kim Topley, O’Reilly, 2002, Chapter 4
Examples in this lecture are based on examples in the above reference
| CS 683 Fall 04 | Doc 20, MIDlet UI pt 2 Slide # 3 | 
The following can be added to a form
| ChoiceGroup | CustomItem | 
| DateField | Gauge | 
| ImageItem | Spacer | 
| StringItem | TextField | 
| CS 683 Fall 04 | Doc 20, MIDlet UI pt 2 Slide # 4 | 
Based on an example from J2ME in a Nutshell
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
   
public class StringItems extends MIDlet {   
   protected Display display;
   
   public StringItems() {
      Form form = new Form("StringItem");
      
      form.append(new StringItem("State ", "OK"));
      form.append(new StringItem(null, "No label\n"));
      form.append(new StringItem(null, "Line\nbreak"));
      form.append(new StringItem("Label", "Text."));
      form.append(new StringItem("Label2 ", "Text2."));
      form.append("\nImplicit String item");
      
      display = Display.getDisplay(this);         
      display.setCurrent(form);
   }
   
   public void startApp() { }
   public void pauseApp() { }
   public void destroyApp( boolean unconditional ) { }
}
| CS 683 Fall 04 | Doc 20, MIDlet UI pt 2 Slide # 5 | 
Based on an example from J2ME in a Nutshell
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
   
public class TextFieldItems extends MIDlet implements ItemStateListener {   
   protected Display display;
   
   public TextFieldItems() {
      Form form = new Form("TextField");
      form.append("Some text fields");
      form.append(
         new TextField("Any", "Hi mom", 8, TextField.ANY));
      form.append(
         new TextField("Phone", "1234567890", 10, 
               TextField.PHONENUMBER));
      form.append(
         new TextField("Number", "12345", 8, 
               TextField.NUMERIC));
      form.append(
         new TextField("Password", null, 8, TextField.PASSWORD | 
               TextField.NUMERIC));
      form.setItemStateListener(this);
      display = Display.getDisplay(this);
      display.setCurrent(form);
   }
| CS 683 Fall 04 | Doc 20, MIDlet UI pt 2 Slide # 6 | 
   public void itemStateChanged(Item item)  {      
      String textEntered =((TextField)item).getString();
      Alert displayNewText = new Alert("Changes",textEntered, null, AlertType.INFO);
      displayNewText.setTimeout(2000);
      display.setCurrent(displayNewText);
   }
   
   public void startApp() { }
   public void pauseApp() { }
   public void destroyApp( boolean unconditional ) { }
}
| CS 683 Fall 04 | Doc 20, MIDlet UI pt 2 Slide # 7 | 
Based on an example from J2ME in a Nutshell
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
   
public class DateFieldItems extends MIDlet implements ItemStateListener
   {   
   protected Display display;
   
   public void itemStateChanged(Item item) 
      {      
      Date newDate =((DateField)item).getDate();
      Calendar accessableDate = Calendar.getInstance();
      accessableDate.setTime( newDate);
      Alert displayNewText = 
         new Alert("Date","" + accessableDate, null, AlertType.INFO);
      displayNewText.setTimeout(2000);
      display.setCurrent(displayNewText);
      }
   
   public void startApp() { }
   public void pauseApp() { }
   public void destroyApp( boolean unconditional ) { }
   
| CS 683 Fall 04 | Doc 20, MIDlet UI pt 2 Slide # 8 | 
   public DateFieldItems()
      {
      Form form = new Form("Dates");
      
      DateField dateOnly = new DateField("Date", DateField.DATE);
      DateField both = new DateField("Both", DateField.DATE_TIME);
      Date now = new Date();
      dateOnly.setDate(now);
      both.setDate(now);
      form.append( both);
      form.append( dateOnly);
      
      //Time only requires date to be Jan 1. 1970, the start of time
      Date beginning = new Date(0);
      Calendar base = Calendar.getInstance();
      base.setTime(beginning);
      Calendar forTimeOnly = Calendar.getInstance();
      forTimeOnly.setTime(now);
      forTimeOnly.set(Calendar.YEAR, base.get(Calendar.YEAR));
      forTimeOnly.set(Calendar.MONTH, base.get(Calendar.MONTH));
      forTimeOnly.set(Calendar.DATE, base.get(Calendar.DATE));
      DateField timeOnly = new DateField("Time", DateField.TIME);
      timeOnly.setDate( base.getTime());
      form.append( timeOnly);
      
      form.setItemStateListener(this);
      display = Display.getDisplay(this);         
      display.setCurrent(form);
      }
   }
| CS 683 Fall 04 | Doc 20, MIDlet UI pt 2 Slide # 9 | 
http://www.eli.sdsu.edu/courses/fall04/cs683/j2me/docs/api/midp/javax/microedition/lcdui/Gauge.html
Based on an example from J2ME in a Nutshell
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
   
public class GaugeItems extends MIDlet implements ItemStateListener
   {   
   protected Display display;
   
   public GaugeItems()
      {
      Form form = new Form("Gauge");
      String label = null;
      int maxValue = 100;
      boolean editable = true;
      int initialValue = 50;
      form.append(new Gauge(label, editable, maxValue, initialValue));
      form.append(new Gauge(label, editable, maxValue, 25));
      form.append(new Gauge(label, false, maxValue, 10));
      
      form.setItemStateListener(this);
      display = Display.getDisplay(this);         
      display.setCurrent(form);
      }
| CS 683 Fall 04 | Doc 20, MIDlet UI pt 2 Slide # 10 | 
   public void itemStateChanged(Item item) 
      {      
      int value =((Gauge)item).getValue();
      Alert displayNewText = 
         new Alert("New Value","" + value, null, AlertType.INFO);
      displayNewText.setTimeout(800);
      display.setCurrent(displayNewText);
      }
   
   public void startApp() { }
   
   public void pauseApp() { }
   public void destroyApp( boolean unconditional ) { }
   
   }
| CS 683 Fall 04 | Doc 20, MIDlet UI pt 2 Slide # 11 | 
http://www.eli.sdsu.edu/courses/fall04/cs683/j2me/docs/api/midp/javax/microedition/lcdui/List.html
Based on an example from J2ME in a Nutshell
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
   
public class Lists extends MIDlet implements CommandListener
   {   
   protected Display display;
   protected List list;   
   private static final Command EXIT_COMMAND = 
                  new Command("Exit", Command.EXIT, 0);
   
   private static final Command OK_COMMAND =
                  new Command("OK", Command.OK, 0);
   
   public Lists()
      {
      String[] options = { "A", "B", "C" };
      Image[] images = null;
      list = new List("Exclusive List", List.EXCLUSIVE, options, images);
      list.addCommand(OK_COMMAND);         
      list.addCommand(EXIT_COMMAND);
      list.setCommandListener(this);
   
      display = Display.getDisplay(this);         
      display.setCurrent(list);
      }
| CS 683 Fall 04 | Doc 20, MIDlet UI pt 2 Slide # 12 | 
   public void commandAction(Command c, Displayable d) 
      {
      if (c == EXIT_COMMAND) 
         {
         destroyApp(true);
         notifyDestroyed();
         } 
      else if (c == OK_COMMAND) 
         {
         int value =list.getSelectedIndex();
         Alert displayNewText = 
            new Alert("Selected","Selected index: " + value, null, AlertType.INFO);
         displayNewText.setTimeout(1000);
         display.setCurrent(displayNewText);
         }
      }
   
   public void startApp()    {}
   public void pauseApp() { }
   public void destroyApp( boolean unconditional ) { }
   
   }
| CS 683 Fall 04 | Doc 20, MIDlet UI pt 2 Slide # 13 | 
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
   
public class ImplicitLists extends MIDlet implements CommandListener
   {   
   protected Display display;
   protected List list;   
   private static final Command EXIT_COMMAND = 
                  new Command("Exit", Command.EXIT, 0);
   
   public ImplicitLists()
      {
      String[] options = { "A", "B", "C" };
      Image[] images = null;
      list = new List("Exclusive List", List.IMPLICIT, options, images);
      list.addCommand(EXIT_COMMAND);         
      list.setCommandListener(this);
   
      display = Display.getDisplay(this);         
      display.setCurrent(list);
      }
   
CS 683 Fall 04 Doc 20, MIDlet UI pt 2 Slide # 14 
   public void commandAction(Command c, Displayable d) 
      {
      if (c == EXIT_COMMAND) 
         {
         destroyApp(true);
         notifyDestroyed();
         } 
      else if (c == List.SELECT_COMMAND) 
         {
         int value =list.getSelectedIndex();
         Alert displayNewText = 
            new Alert("Selected","Selected index: " + value, null, AlertType.INFO);
         displayNewText.setTimeout(1000);
         display.setCurrent(displayNewText);
         }
      }
   
   public void startApp() { }
   public void pauseApp() { }
   public void destroyApp( boolean unconditional ) { }
   
   }
| CS 683 Fall 04 | Doc 20, MIDlet UI pt 2 Slide # 15 | 
Based on an example from J2ME in a Nutshell
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
   
public class ChoiceItems extends MIDlet implements ItemStateListener
   {   
   protected Display display;
   
   public ChoiceItems()
      {
      Form form = new Form("Choice");
      String[] strings = new String[] { "Red", "Green", "Blue" };
      Image[] images = null;
      ChoiceGroup exGroup = new ChoiceGroup("Choose one", 
               ChoiceGroup.EXCLUSIVE, strings, images);
      form.append(exGroup);
   
      ChoiceGroup multiGroup =
          new ChoiceGroup("Choose any", ChoiceGroup.MULTIPLE);
      form.append(multiGroup);
      multiGroup.append("A", null);
      multiGroup.append("B", null);
      multiGroup.append("C", null);
      
      form.setItemStateListener(this);
      display = Display.getDisplay(this);
      display.setCurrent(form);
      }
| CS 683 Fall 04 | Doc 20, MIDlet UI pt 2 Slide # 16 | 
   public void itemStateChanged(Item item) 
      {      
      Choice choice =(Choice)item;
      int count = choice.size();
      boolean[] states = new boolean[count];
      int selCount = choice.getSelectedFlags(states);
      if (selCount > 0) 
         {
         String selected = "Selected: ";
         for (int i = 0; i < count; i++) 
            if (states[i]) selected += choice.getString(i) + " ";
         Alert displaySelected = 
            new Alert("Selected",selected, null, AlertType.INFO);
         displaySelected.setTimeout(800);
         display.setCurrent(displaySelected);
         }
   
      int selectedIndex = choice.getSelectedIndex();
      Alert displaySelected = 
         new Alert("Index ","" + selectedIndex, null, AlertType.INFO);
      displaySelected.setTimeout(1000);
      display.setCurrent(displaySelected);
      }
   
   public void startApp() { }
   public void pauseApp() { }
   public void destroyApp( boolean unconditional ) { }
   }
| CS 683 Fall 04 | Doc 20, MIDlet UI pt 2 Slide # 17 | 
Manifest-Version: 1.0 MIDlet-7: ChoiceItems, , ChoiceItems MIDlet-6: ImplicitLists, , ImplicitLists MIDlet-5: Lists, , Lists MIDlet-4: GaugeItems, , GaugeItems MIDlet-3: DateFieldItems, , DateFieldItems MIDlet-2: TextFieldItems, , TextFieldItems MIDlet-1: StringItems, , StringItems MIDlet-Name: StringItems MIDlet-Version: 1.00.00 MIDlet-Vendor: mpowers LLC MIDlet-Icon: /hello.png MIDlet-Info-URL: http://mpowers.net/ MicroEdition-Configuration: CLDC-1.0 MicroEdition-Profile: MIDP-1.0
| CS 683 Fall 04 | Doc 20, MIDlet UI pt 2 Slide # 18 | 
MIDlet-7: ChoiceItems, , ChoiceItems MIDlet-6: ImplicitLists, , ImplicitLists MIDlet-5: Lists, , Lists MIDlet-4: GaugeItems, , GaugeItems MIDlet-3: DateFieldItems, , DateFieldItems MIDlet-2: TextFieldItems, , TextFieldItems MIDlet-1: StringItems, , StringItems MIDlet-Name: StringItems MIDlet-Version: 1.00.00 MIDlet-Vendor: mpowers LLC MIDlet-Jar-URL: MyProj.jar MIDlet-Jar-Size: 12939 MIDlet-Description: A simple sample. MIDlet-Icon: /hello.png MIDlet-Info-URL: http://mpowers.net/ Title: Item Examples Message: This is a simple sample midlet
Copyright ©, All rights reserved.
2004 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.