/* This project includes some functions such as inserting, appending and replacing items on a form */ import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class FormItems extends MIDlet implements CommandListener{ // Declare objects private Display display; private Form Main; private Command cmInsert; // insert item command private DateField dfDate; // display date private TextField tfSize; // prduct size private TextField tfQuant; // product quantity private int DateIndex; // index of dfDate private Command Exit; String s1 = "Size"; String s2 = "Large"; public FormItems(){ display = Display.getDisplay(this); // create date a retrieve date dfDate = new DateField(" ", DateField.DATE); dfDate.setDate(new java.util.Date()); // Define two textfields and three commands tfSize = new TextField(s1,s2, 5, TextField.ANY); tfQuant = new TextField("Quantity: ", "3", 2, TextField.NUMERIC); cmInsert = new Command("Insert", Command.SCREEN, 1); Exit = new Command ("Exit", Command.EXIT, 1); // create the form, add insert command Main = new Form("Form Items"); Main.addCommand(cmInsert); Main.addCommand(Exit); // Append date to form and save index value where it was inserted DateIndex = Main.append(dfDate); // capture events Main.setCommandListener(this); } //Start midlet public void startApp(){ display.setCurrent(Main); } // pause application public void pauseApp(){ } public void destroyApp(boolean unconditional){ } public void commandAction(Command c, Displayable s){ if (c == Exit){ destroyApp(false); notifyDestroyed(); } if (c == cmInsert){ // one item on form, insert textfield before datfield if (Main.size() ==1){ Main.insert(DateIndex, tfQuant); DateIndex +=1; // date index has changed update } // else if two items and last item is datefield, replace it else if (Main.size() == 2 && Main.get(1) == dfDate) Main.set(DateIndex, tfSize); } } }