Text Field Midlet Example

This example illustrates how to insert text field in your form. We are using here two TextField (name and company). We are taking both TextField variable in to the constructor (TextFieldExample) of the class.

Text Field Midlet Example

Text Field MIDlet Example

     

This example illustrates how to insert text field in your form. We are using here two TextField (name and company). We are taking both TextField variable in to the constructor (TextFieldExample) of the class. When application will be run then first of all the startApp() method will called and it display the form with name and company field. The TextField is in the javax.microedition.lcdui package, has the following methods:

  • delete(int offset, int length) 
  • getCaretPosition() 
  • getChars(char[] data)
  • getConstraints()
  • getMaxSize() 
  • getString()
  • insert(char[] data, int offset, int length, int position) 
  • insert(String src, int position)
  • setChars(char[] data, int offset, int length) 
  • setConstraints(int constraints) 
  • setMaxSize(int maxSize) 
  • setString(String text) 
  • size() 

Application display as follows:

 

 

Source code of TextFieldExample.java is as follows:

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class TextFieldExample extends MIDlet implements CommandListener{
  private Form form;
  private Display display;
  private TextField name, company;
  private Command ok;
  
  public TextFieldExample(){
  name = new TextField("Name:"""30, TextField.ANY);
  company = new TextField("Company Name:"""30, TextField.ANY);
  ok = new Command("OK", Command.OK, 2);
  }

  public void startApp(){
  display = Display.getDisplay(this);
  Form form = new Form("Text Field");
  form.append(name);
  form.append(company);
  form.addCommand(ok);
  form.setCommandListener(this);
  display.setCurrent(form);
  }

  public void pauseApp(){
  
  }

  public void destroyApp(boolean destroy){
  notifyDestroyed();
  }

  public void showInput(){
  display = Display.getDisplay(this);
  String n = name.getString();
  String c = company.getString();
  Form form = new Form("Input Value");
  form.append(n);
  form.append(c);
  display.setCurrent(form);
  }


  public void commandAction(Command c, Displayable d) {
  String label = c.getLabel();
  if(label.equals("OK")){
  showInput();
  
  }
}

 

Download Source Code