Mega Code Archive

 
Categories / Java Tutorial / J2ME
 

Extends Form to create a new form

import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; import javax.microedition.lcdui.Choice; import javax.microedition.lcdui.ChoiceGroup; 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.Item; import javax.microedition.lcdui.ItemStateListener; import javax.microedition.lcdui.TextField; import javax.microedition.lcdui.Ticker; import javax.microedition.midlet.MIDlet; class EntryForm extends Form {   private TextField symbolField = new TextField("Investment Symbol", "", 6, TextField.ANY);   private Command exitCommand = new Command("Exit", Command.EXIT, 1);   private Command getCommand = new Command("Get", Command.SCREEN, 2);   private ChoiceGroup investmentChoice = new ChoiceGroup("Type", Choice.EXCLUSIVE, new String[]{ "Stock", "Fund" }, null);   public EntryForm(String title) {     super(title);     append(symbolField);     append(investmentChoice);     addCommand(exitCommand);     addCommand(getCommand);   }   public TextField getSymbolField() {     return symbolField;   }   public ChoiceGroup getInvestmentChoice() {     return investmentChoice;   }   public Command getExitCommand() {     return exitCommand;   }   public Command getGetCommand() {     return getCommand;   } } public class ObtainQuoteMIDlet extends MIDlet {   private Display displayMngr = null;   private EntryForm entryForm = null;   private Alert resultsAlert = null;   private Ticker adTicker = new Ticker("Track your investments");   public ObtainQuoteMIDlet() {   }   private void initListener() {     ItemStateListener itemListener = new ItemStateListener() {       public void itemStateChanged(Item item) {         if ((item == entryForm.getInvestmentChoice())             && (entryForm.getInvestmentChoice().getSelectedIndex() == 1)             && !(entryForm.getSymbolField().getString().toUpperCase().endsWith("X"))) {           Alert symbolAlert = new Alert("Check Symbol", "Mutual Funds end in 'X'", null,               AlertType.WARNING);           symbolAlert.setTimeout(Alert.FOREVER);           displayMngr.setCurrent(symbolAlert, entryForm);         }       }     };     CommandListener commandListener = new CommandListener() {       public void commandAction(Command c, Displayable d) {         if (c == entryForm.getExitCommand()) {           destroyApp(true);         } else if (c == entryForm.getGetCommand()) {           if ((entryForm.getInvestmentChoice().getSelectedIndex() == 1)               && !(entryForm.getSymbolField().getString().toUpperCase().endsWith("X"))) {             Alert symbolAlert = new Alert("Check Symbol", "Mutual Funds end in 'X'", null,                 AlertType.WARNING);             symbolAlert.setTimeout(Alert.FOREVER);             displayMngr.setCurrent(symbolAlert, entryForm);           } else {             if (entryForm.getSymbolField().getString().length() > 0) {               String sym = entryForm.getSymbolField().getString();               displayPrice("The price of " + sym + " is $111.19");             }           }         }       }     };     entryForm.setItemStateListener(itemListener);     entryForm.setCommandListener(commandListener);   }   private void displayEntryForm() {     if (entryForm == null) {       entryForm = new EntryForm("ObtainQuote");     }     initListener();     displayMngr.setCurrent(entryForm);   }   private void displayPrice(String quoteString) {     if (resultsAlert == null) {       resultsAlert = new Alert("Quote Price", null, null, AlertType.CONFIRMATION);       resultsAlert.setTicker(adTicker);       resultsAlert.setTimeout(Alert.FOREVER);     }     resultsAlert.setString(quoteString);     displayMngr.setCurrent(resultsAlert, entryForm);   }   protected void startApp() {     displayMngr = Display.getDisplay(this);     displayEntryForm();   }   protected void pauseApp() {   }   protected void destroyApp(boolean unconditional) {     notifyDestroyed();   }   public void commandAction(Command c, Displayable s) {   } }