A form with image and calender options for a mobile device

Hi fellows 🙂 My today code sample is going to be little bit lengthy one. The things you can learn from here are;

  • How to include an image in to a form
  • A choice group feature
  • Date and calendar option
  • Gauge option

The classes that you will be going to meet newly in this post are;

  • AlertType
  • ChoiceGroup
  • Date
  • DateField
  • Displayable
  • Gauge
  • Image
  • ImageItem
  • IOException
  • Item
  • Spacer
  • Thread
  • TimeZone

The interfaces which is newly included in this post are;

  • Choice

The scenario is like this. When you run the program you will be provided an interface where a nice looking image and choice group. There you can select your choice like in Figure 1.

First Interface

Figure 1

If you select Date option, you will be direct to interface like below. Here you can select date and time from the calendar and set it as the your date. See the Figure 2 below.

Calender Interface
Figure 2

When you press the OK command, you can see the date that you have selected in an alert box (first phone interface in Figure 3). Then press Back command to go to the initial interface. Now select the other option, which is Gauge, from the choice group. Then you can see the following interface (second phone interface in Figure 3).

Gauge Interface
Figure 3

Now lets see the precious thing, which is the code behind all of these.

package anuja;

import java.io.IOException;
import java.util.Date;
import java.util.TimeZone;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.Spacer;
import javax.microedition.midlet.*;

 * @author Anuja
public class Calender extends MIDlet implements CommandListener {

    private Form selectForm;
    private Display display;
    private Image formImage;
    private ImageItem imageItem;
    private ChoiceGroup choiceGrp;
    private Image choiceImage;
    private Command exitCmd;
    private Command goCmd;
    private Form dateForm;
    private DateField dateField;
    private Command backCmd;
    private Form gaugeForm;
    private Gauge gauge;
    private Thread gaugeThread;
    private Command okCmd;
    private Date dateInfo;
    private Alert dateAlert;
    private Spacer space;

    public void startApp() {

        // ---- interface with image and choice group --------------------

        display = Display.getDisplay(this);
        selectForm = new Form("Select Your Choice");

        try {
            formImage = Image.createImage("funny.png");
            choiceImage = Image.createImage("happy.png");
        } catch (IOException ex) {

        imageItem = new ImageItem("Your Choice \n", formImage, Item.LAYOUT_CENTER, "funny");

        space = new Spacer(200, 50); // width, height

        choiceGrp = new ChoiceGroup("Select Here", Choice.POPUP);
        choiceGrp.append("Date", null);
        choiceGrp.append("Gauge", choiceImage);

        exitCmd = new Command("Exit", Command.EXIT, 7);
        goCmd = new Command("Go", Command.OK, 4);


        // ----------------------- interface with calendar ---------------------

        dateForm = new Form("Select Date");
        dateField = new DateField("Select Date \n", DateField.DATE_TIME, TimeZone.getTimeZone("GMT+5.00"));
        dateField.setDate(new Date());

        backCmd = new Command("Back", Command.BACK, 2);
        okCmd = new Command("OK", Command.OK, 4);


        // ------------------------ interface with gauge -----------------------

        gaugeForm = new Form("See Gauge");
        // If the second argument (boolean value) is false, we can't manually change the gauge
        gauge = new Gauge("Volume Controller \n", true, 10, 3);
        // argument 1=left align; 2=right align; other than 1,2,3=java.lang.IllegalArgumentException

        gaugeThread = new Thread() {

            public void run() {
                for (int i = 3; i <= 10; i++) {
                    try {
                    } catch (InterruptedException ex) {


    public void pauseApp() {

    public void destroyApp(boolean unconditional) {

    public void commandAction(Command c, Displayable d) {
        if (c == exitCmd) {
        } else if (c == goCmd) {
            if (choiceGrp.getSelectedIndex() == 0) {
            } else {
        } else if (c == backCmd) {
        } else if (c == okCmd) {
            dateInfo = dateField.getDate();
            dateAlert = new Alert("Selected Date: ", dateInfo.toString(), null, AlertType.INFO);
            display.setCurrent(dateAlert, dateForm);

The best thing you can do is copy the whole code and paste it in the Netbeans IDE and examine the code. Then with the different colors, you will be able to clearly differentiate comments and the code.

Thank You 🙂

About AnujAroshA

Working as an Associate Technical Lead. Specialized in iOS application development. A simple person :)
This entry was posted in J2ME examples. Bookmark the permalink.

1 Response to A form with image and calender options for a mobile device

  1. Rahul Kumar says:

    Program is Successfully running but sir I see your Image of phone color full ,Background also color full in my not be show color full and use to develop the application in eclipse-SDK-3.7.1-win32 and sun_java_wireless_toolkit-2_5_2-ml-windows. and your some program not work in my eclipse I show my problem in previous comment Plz give me suitable answers in my Email id.

    Sir I am beginner to study the J2me Plz tell me any practical Book It have more of the program and Application i easily understand the developed the own application plz sir Help me.

    Email Id: chauhans13@gamail.com, rahulchauhan.mca1@gmail.com

Leave a Reply

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

WordPress.com Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s