Complete source code of ContactBookDemo application

And here is the complete source code we created. Note that you can clean up most LWUIT imports using simply import com.sun.lwuit.*;.

import com.sun.lwuit.*;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.list.DefaultListModel;
import java.util.Enumeration;
import javax.microedition.midlet.MIDlet;
import javax.microedition.pim.Contact;
import javax.microedition.pim.ContactList;
import javax.microedition.pim.PIM;
import javax.microedition.pim.PIMException;

public class Midlet extends MIDlet {

    private List contactList = null;
    private PIM pim = null;
    private Command clearCommand;


    public void startApp() {
        Display.init(this);
        
        Form form = new Form("Hello World");
        form.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        contactList = new List();
        form.addComponent(contactList);
        pim = PIM.getInstance();
        
        // add search field and search button
        final TextArea searchField = TextField.create();
        form.addComponent(searchField);
        Button searchButton = new Button("Search");
        form.addComponent(searchButton);
        searchButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                populateContactList(searchField.getText());
				 }
        });
        populateContactList(searchField.getText());

        
        //add exit command
        Command exitCommand = new Command("Exit") {
            public void actionPerformed(ActionEvent e) {
                notifyDestroyed();
            }
        };
        form.addCommand(exitCommand);
        form.setBackCommand(exitCommand);

       // add clear command
        populateContactList(searchField.getText());
        clearCommand = new Command("Clear") {
            public void actionPerformed(ActionEvent evt) {
                searchField.setText("");
                populateContactList("");
            }
        };
        form.addCommand(clearCommand);
 
       form.show();
       form.setFocused(searchField);
    }

    // populate contact list (names retrieved from the phonebook)
    private void populateContactList(String searchTerm) {
        contactList.setModel(new DefaultListModel());
        try {
            String[] pimListNames = pim.listPIMLists(PIM.CONTACT_LIST);
            for (int i = 0; i < pimListNames.length; ++i) {
                ContactList cl = (ContactList) pim.openPIMList(
                        PIM.CONTACT_LIST, PIM.READ_ONLY, pimListNames[i]);
                Enumeration items = cl.items(searchTerm);
                while (items.hasMoreElements()) {
                    Contact c = (Contact) items.nextElement();
                    contactList.addItem(c.getString(Contact.FORMATTED_NAME, 0));
                }
            }
        } catch (PIMException ex) {
            ex.printStackTrace();
        }
        if (contactList.getModel().getSize() == 0) {
            contactList.addItem("No matches");
        }
    }
    
       
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }
}