Implementing Phone Book application in J2ME (step-1)

Actually I wanted to do this in a single blog post but then I thought to divide it to several posts for others to understand. So in this post you can learn how to add a contact to a Choice Group. The classes that you will going to meet newly in this post are;

  • RecordStore
  • RecordStoreException
  • RecordStoreNotOpenException

The Interfaces that I’m going to use in this blog but did not used in one of my earlier posts are;

  • RecordEnumeration

Here is the interfaces that I am going to implement.

First Interface
Figure 1

Before search you have to add some contacts. But normally when we insert a SIM to a phone there are some numbers and they will show in a real phone book application. Anyway, to add a new contact click on the Add command.

Second Interface
Figure 2

You can enter a Name and the Phone Number attributes for a new contact. If you don’t want to add a contact, then you can go to previous interface by pressing the Back command. If not after providing the details click on the Save command.

Third Interface
Figure 3

You will get a Alert message after a new contact added and it will redirect you to the first interface. Now you can see the contact that you have added. In this post I have not implemented Search option and I will show it in my next post. Till then let’s see the code behind this story.

package anuja;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
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;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

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

    private Display display;
    private ChoiceGroup searchChoice;
    private Form searchForm;
    private TextField searchTxtField;
    private Command exitCmd;
    private Command addContactCmd;
    private Form addContactForm;
    private TextField nameTxtField;
    private TextField numberTxtField;
    private Command backCmd;
    private Command saveCmd;
    private String nameStr;
    private String numberStr;
    private Alert addAlert;
    private RecordStore openRecStore;

    public void startApp() {
        display = Display.getDisplay(this);

        // --------------------- Search Contact Form ---------------------------

        searchForm = new Form("Search Contact");
        searchTxtField = new TextField("Search Here", "", 30, TextField.ANY);

        searchChoice = new ChoiceGroup("", Choice.EXCLUSIVE);
        //searchChoice.append("Anuja", null);
        //searchChoice.append("Arosha", null);

        exitCmd = new Command("Exit", Command.EXIT, 7);
        addContactCmd = new Command("Add", Command.OK, 4);


        //-------------------- Add Contact Form --------------------------------

        addContactForm = new Form("Add Contacts");
        nameTxtField = new TextField("Name", "", 80, TextField.ANY);
        numberTxtField = new TextField("Number", "", 20, TextField.PHONENUMBER);

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


        //-------------------- Open a RecordStore ------------------------------

        try {
            openRecStore = RecordStore.openRecordStore("Contacts", true);
        } catch (RecordStoreException ex) {


    public void pauseApp() {

    public void destroyApp(boolean unconditional) {

    public void commandAction(Command c, Displayable d) {
        if (c == exitCmd) {
        } else if (c == addContactCmd) {
        } else if (c == backCmd) {
        } else if (c == saveCmd) {

            //------------------ Get the inputs ------------------------
            nameStr = nameTxtField.getString();
            numberStr = numberTxtField.getString();
            String fullDetails = nameStr + "*" + numberStr;

            // Can add the name to the ChoiceGroup like below or by getting it from the RecordStore like in loadContacts()
            //searchChoice.append(nameStr, null);

            addAlert = new Alert("Added to the Contacts");
            display.setCurrent(addAlert, searchForm);


//--- To add to the RecordStore we need to convert the String to byte array ---
            byte[] byteArray = fullDetails.getBytes(); // String class

//--- Add a new record to the RecordStore
            try {
                openRecStore.addRecord(byteArray, 0, fullDetails.length()); // RecordStore class
            } catch (RecordStoreException ex) {

            // If you remove the following line, the newly adding contacts are not display. They will display after another contact being added

    private void loadContacts() {
        // Add following line to ignore the same contact add twice

        try {
//---  Returns an enumeration for traversing a set of records in the record store in an optionally specified order.
            RecordEnumeration enuRec = openRecStore.enumerateRecords(null, null, true);

            while (enuRec.hasNextElement()) {
                try {
                    // Take a recoed and convert it from byte array to String
                    byte[] nextRec = enuRec.nextRecord();
                    String nextRecStr = new String(nextRec);

                    // Seperate Name and the Phone number from the String
                    String takeName = nextRecStr.substring(0, nextRecStr.indexOf("*"));
                    String takeNumber = nextRecStr.substring(nextRecStr.indexOf("*") + 1, nextRecStr.length());

                    // Add the Name in to ChoiceGroup
                    searchChoice.append(takeName, null);
                } catch (RecordStoreException ex) {
        } catch (RecordStoreNotOpenException ex) {

I just copied and paste my whole code here and hope everything copied well without any mistakes😉 Feel free to copy the code and edit it. The only thing we expect finally is to learn something from a blog post.

Will meet with the “Part – 2”🙂

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.

8 Responses to Implementing Phone Book application in J2ME (step-1)

  1. Anandkumar S says:

    How to delete and edit phone book, Please help with code

  2. Rahul Kumar says:

    How to make full Phonebook with have a multiple Opation.

  3. Rahul Kumar says:

    How to make the Message Box have the Message create,email create,Inbox with all the option,outbox,send box,drafts,templates,Delete all and setting.

    • anujarosha says:


      I just gave an idea in this post and the next two posts. You are talking about a whole application which is I am not interested to post. You have to do the coding and ask the questions when you get errors. Sorry😦

  4. Rahul Kumar says:

    plz quick the answers in my email id.

    • i want a phone book in java (J2ME) which can store 3-4 phone numbers per person
      and it will be jar application that will increase phone book capacity of my phone and will store phone book data on SDMMC card. It will also direct provide dial / sms option directly from jar application – Do you know any ?

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