Simple Form Application in J2ME

Hi guys, as the beginning of learning J2ME with mobile, lets start with a simple form application. Here you will be able to get an understand of the following classes;

  • Alert
  • Command
  • Display
  • Form
  • MIDlet
  • TextField

Other than that you are going to meet CommandListner interface too. Because if you are going to use Command class you have to implement that interface.

First I will show you the GUI which I am going to implement. There is one thing I have to

Initial Screen
Figure 1

show you with my emulator. That is the additional buttons are hide under Menu and when you click on that you can see those buttons. But after I install the application in my mobile phone it shows as I expected. There is another contraindication between the words “button” and “command”  when we are in this domain. As you can see there is Exit component in the Figure 1. Some identify it as a button, but here after I will use command for those items.

In my previous blog post I just told how to create a new project for mobile applications. But to create interface like in Figure 1 you have to create a MIDlet. This is the procedure to that.

You can see the created project in your Projects window of your NetBeans IDE. Expand it by clicking on the + icon. Then right click on Source Packages select New -> MIDlet. Then give a MIDlet name and the MIDlet class name. I will explain how to add an icon later. For now complete the form by providing a Package name. Then it will create a Java file with the name of the MIDlet class you have provided above. Below is the code to implement Figure 1 interface.

package anuja;

import javax.microedition.lcdui.Alert;
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.TextField;
import javax.microedition.midlet.MIDlet;

 * @author Anuja
public class ContactForm extends MIDlet implements CommandListener {
    private Display display;
    private Form contactForm;
    private TextField nameTxtFld;
    private Command storeCmd;
    private Alert alert;
    private Command exitCmd;

    public void startApp() {
        display = Display.getDisplay(this);
        contactForm = new Form("Contact Form");
        nameTxtFld = new TextField("Name", "", 30, TextField.ANY);

        storeCmd = new Command("Store", Command.OK, 4);
        exitCmd = new Command("Exit", Command.EXIT, 7);


    public void pauseApp() {

    public void destroyApp(boolean unconditional) {

    public void commandAction(Command c, Displayable d) {
        if(c == storeCmd){
            alert = new Alert("Working");
            display.setCurrent(alert, contactForm);
        }else if(c == exitCmd){

Then save the code and run the project. In my IDE F6 is the short-cut key to run the application in my emulator. It will work now. I am not going to extend this post any further. Therefore I will explain how to add that icon and how to deploy an application to your phone, in another post. Till then Bye🙂

About AnujAroshA

An undergraduate in the stream of ICT (Information & Communication Technology). A simple person :)
This entry was posted in J2ME examples. Bookmark the permalink.

One Response to Simple Form Application in J2ME

  1. suumitgeek says:

    Hey Anuj thank you for the tutorial :p

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s