Programming Tutorials

TextBox sample program in J2ME

By: Priya in J2ME Tutorials on 2007-09-16  

Here's an example program that demonstrates the use of TextBox in J2ME:

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;

public class TextBoxExample extends Form implements CommandListener {
    private TextBox textBox;
    private Command okCommand;
    private Command backCommand;
    private Display display;

    public TextBoxExample(String title, Display display) {
        this.display = display;

    private void init() {
        textBox = new TextBox("Enter your name:", "", 20, TextField.ANY);
        okCommand = new Command("OK", Command.OK, 1);
        backCommand = new Command("Back", Command.BACK, 2);

    public void commandAction(Command command, Displayable displayable) {
        if (command == okCommand) {
            String name = textBox.getString();
            append("Hello, " + name + "!");
        } else if (command == backCommand) {

In this example, we create a TextBox that prompts the user to enter their name. The TextBox is added to a Form, which is then added to the display. We also create two commands: OK and Back, which allow the user to submit their name and go back to the previous screen, respectively.

When the user enters their name and selects the OK command, the commandAction() method is called, which retrieves the user's name from the TextBox and displays a greeting message on the screen.

This is just a simple example, but TextBox can be used for a variety of input scenarios in J2ME applications.

Add Comment

* Required information


No comments yet. Be the first!

Most Viewed Articles (in J2ME )

Latest Articles (in J2ME)