View RSS Feed

My Java Tips

TextField vs TextBox

Rate this Entry
by , 11-22-2011 at 06:08 PM (2520 Views)
javax.microedition.lcdui provides TextField and TextBox which are sometimes confusing. Both are to allow the user to enter text, but question arises, when to use which one? In this post, I will try to address this issue.


Let me first explain TextField (javax.microedition.lcdui.TextField). This class inherits from javax.microedition.lcdui.Item, so you should know that its an item that can be placed on a form. To create an instance of TextField, we use the TextField constructor:

Java Code:
TextField(String label, String text, int maxSize, int constraints)
The parameters are self explanatory. Let me present an example:

Java Code:
...
Display display;
Form form;
TextField textField;
...
protected void startApp() throws MIDletStateChangeException {
System.out.println("startApp.");
display = Display.getDisplay(this);
form = new Form("Helloworld example");
StringItem body = new StringItem(null,"Hello World!");
textField = new TextField("Name", "your last name", 15, 0);
form.append(body);
form.append(textField);
display.setCurrent(form);
Output:


So use Textfield when you want to take input from the user and want to place the field on a form.

I will write about TextBox (javax.microedition.lcdui.TextBox). This class inherits from javax.microedition.lcdui.Screen, so you should know that its a Screen and unlike TextField, it cannot be appended to a Form.



To create an instance of TextBox, we use the TextBox constructor:


TextBox(String label, String text, int maxSize, int constraints)

The parameters are self explainatory. Let me present an example:

Java Code:
...
Display display;
TextBox textBox;
...
protected void startApp() throws MIDletStateChangeException {
System.out.println("startApp.");
display = Display.getDisplay(this);
form = new Form("Helloworld example");
StringItem body = new StringItem(null,"Hello World!");
textBox = new TextBox("Location", "your location", 15, 0); 

display.setCurrent(textBox);
Output:
TextBox example


So use TextBox when you want to take input from the user without using Form. Since TextBox is a Screen, you may append commands on it as well.

Submit "TextField vs TextBox" to Facebook Submit "TextField vs TextBox" to Digg Submit "TextField vs TextBox" to del.icio.us Submit "TextField vs TextBox" to StumbleUpon Submit "TextField vs TextBox" to Google

Categories
Java ME

Comments