Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home J2me Text Field Midlet Example

Related Tutorials


 
 

Share on Google+Share on Google+

Text Field Midlet Example

Advertisement
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

     

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

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 3, 2008

Related Tutorials

Discuss: Text Field Midlet Example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
moges damene
June 3, 2011
computer science

many thanks for you release this information.
Yasin
July 8, 2014
code adjustment

brother i have a code of sending sms , it is working but i want that if i sent message that wil be save like this text field example. please help me to solve my problem thanks
DMCA.com