TextBox sample program in J2ME

By: Priya Emailed: 1598 times Printed: 2044 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Unlike a TextField, a TextBox is a multiline, editable text area. TextBox is a kind of Screen, not an Item. Because a TextBox is a Displayable, you must create a MIDlet object to demonstrate its use; you can't place it in another Screen or Form, as you can with the components derived from Item.

The program below shows the partial source code of the TextBoxDemo class. The parts that are omitted are very similar structurally to the UIComponentDemo code, and relate to the attributes of the MIDlet.

You can see from the constructor that a TextBox is similar to a TextField, except that it's a
multiline text area. The arguments are the title, the initial text, the maximum number of characters
that it can hold, and the input constraints. The constraints are exactly the same constraints used by
the TextField class.

//Text boxes are screens and don't need a form in which to exist.
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.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;

/**
This MIDlet demonstrates use of the MIDP UI TextBox
Displayable.
98
@see javax.microedition.lcdui.TextBox
*/
public class TextBoxDemo extends MIDlet
implements CommandListener
{
private Command quit =
new Command("Exit", Command.EXIT, 1);
private static TextBoxDemo instance;
// The TextBox UI component.
private TextBox textBox;
// The maximum number of characters that the TextBox can
// hold.
private int MAX_SIZE = 100;
// The TextBox's initial text.
private String initialText =
"You can edit the contents of this TextBox";

/**
Constructor.
*/
public TextBoxDemo()
{
super();
instance = this;
}
public void pauseApp()
{
}
public void destroyApp(boolean destroy)
{
textBox = null;
initialText = null;
instance = null;
}
void quit()
{
destroyApp(true);
notifyDestroyed();
}
public void startApp()
{
textBox = new TextBox("A TextBox",
initialText,
MAX_SIZE,
TextField.ANY);
textBox.addCommand(quit);
textBox.setCommandListener(this);
display();
}

/**
Returns the single instance of this class. Calling
this method before constructing an object will return
a null pointer.
@return an instance of this class.
*/
public static TextBoxDemo getInstance()
{
return instance;
}
public void display()
{
Display.getDisplay(this).setCurrent(textBox);
}
public void commandAction(Command c, Displayable d)
{
if (c == quit)
{
quit();
}
}
}

Just as with other editable objects, you simply select the TextBox using the emulator's Select button and then edit the contents. You can navigate using the four arrow keys, erase characters using the Clear key, and input using either the keypad keys or your computer keypad when you're using an emulator. Of course, the program can also manipulate the content using a rich API that supports inserting, deleting, setting the maximum size, setting constraints, and so forth.


J2ME Home | All J2ME Tutorials | Latest J2ME Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Priya
Getting started with ASP
EJB Entity Beans
Error Handling - Stderr and Exit - sample program in C
Pointers and Arrays in C
Data Types and Sizes in C
virtual inheritance example in C++
ctime() sample program in C++
TextBox sample program in J2ME
Advantages of Java Beans
list() contents of a Directory - sample program in Java
File example program in Java
continue and break statements in C++
Use of 'throw' in Java
Passing parameters to a function by value in C++
Demonstrating global and local variables in C++

More Tutorials in J2ME
Code sample to Send SMS from a J2ME application.
Adding your own Application icon for your J2ME application (jar file)
Play a multimedia file in J2ME Program (Audio/Video) using MMAPI
Datagrams in J2ME (UDP Programming sample)
Client Server in J2ME (Socket Programming sample)
Using HttpConnection in J2ME (Retrieve web content from a website to a phone)
Using HTTP vs UDP vs Socket in J2ME
RMSCookieConnector - Using Cookies in J2ME
POST UTF-8 encoded data to the server in J2ME
lists, forms, choices, gauges, text fields, text boxes in J2ME
Using List to create a Menu and Menu items in J2ME
Using alerts and tickers in J2ME
J2ME Canvas sample to show games programming in J2ME
Timer and TimerTask example in J2ME
List of GPRS Access points for all countries

More Latest News
Most Viewed Articles (in J2ME )
Types of configurations in J2ME
Sample program to draw a arc in J2ME
A sample J2ME midlet that shows an example of using a Timer and a TimerTask.
Your first J2ME program and a midlet lifecycle explained.
java.lang.SecurityException: MIDP lifecycle does not support system exit.
RMS Basics in J2ME
Using HttpConnection in J2ME (Retrieve web content from a website to a phone)
Adding your own Application icon for your J2ME application (jar file)
Types of Profiles in J2ME
Client Server in J2ME (Socket Programming sample)
What is J2ME?
Getting Started with J2ME
What is J2ME?
How to load J2ME applications to the IDEN handsets
'LINK.EXE' is not recognized as an internal or ext
Most Emailed Articles (in J2ME)
What is J2ME?
Y.S. Sun Green Building Research Center
UDP Datagram sample in J2ME
POST UTF-8 encoded data to the server in J2ME
Datagrams in J2ME (UDP Programming sample)
Download a file over a network in J2ME midlet
How to load J2ME applications to the IDEN handsets
'LINK.EXE' is not recognized as an internal or ext
Sample Java program shows how to Read a file over a network using J2ME midlet
Sample J2ME code that shows various functionality of RMS.
A sample J2ME midlet that shows an example of using a Timer and a TimerTask.
GUI components and menu based J2ME Applications.
Your first J2ME program and a midlet lifecycle explained.
java.lang.SecurityException: MIDP lifecycle does not support system exit.
How to get the CLDC and MIDP version from a J2ME program