List and ListModel creation

List uses ListModel to represent the data structure of the list. It allows a list to represent any potential data source by referencing different implementations of this interface. For example, a list model can be implemented in such a way that it retrieves data directly from storage (although caching is recommended). It is the responsibility of the list to notify observers (specifically the view List) of any changes to its state (items removed, added, or changed, and so forth) so that the data is updated on the view.

Figure: A basic List with four items

Initializing a List

You can create a list in one of four ways:

List()

Creates a new instance of List with an empty default model.

List(ListModel model)

Creates a new instance of List with the given model.

List(Object[] items)

Creates a new instance of List with an array of Objects that are placed into the list model.

List(Vector items)

Creates a new instance of List where a set of items are placed into the list model.

Creating a Model

There are two ways to create a list model:

DefaultListModel

Everything is taken care of for you.

ListModel

Implement the list model interface (use a general purpose implementation of the list model interface derived from the DefaultListModel).

DefaultListModel

The following code demonstrates using the DefaultListModel class with a vector of elements.

// Create a set of items
String[] items = { "Red", "Blue", "Green", "Yellow" };

// Initialize a default list model with “item” inside
DefaultListModel myListModel = new DefaultListModel(items);

// Creating a List with “myListModel”
List list = new List(myListModel);

You can customise your ListModel easily by extending the DefaultListModel. See the example “List with list items having title and subtext” under Custom list types section for further reference.

ListModel

You can create your own ListModel from scratch by implementing the ListModel interface. Here is a basic ListModel implementation that uses a Vector as the model storage:

public class MyListModel implements ListModel {

    private Vector itemStore;
    private int selectedIndex;
    private Vector dataChangedListeners;
    private Vector selectionListeners;

    public MyListModel() {
        itemStore = new Vector();
        dataChangedListeners = new Vector();
        selectionListeners = new Vector();
    }

    public Object getItemAt(int i) {
        return itemStore.elementAt(i);
    }

    public int getSize() {
        return itemStore.size();
    }

    public int getSelectedIndex() {
        return selectedIndex;
    }

    public void setSelectedIndex(int i) {
        int oldSelectedIndex = selectedIndex;
        if (selectedIndex != i) {
            selectedIndex = i;
            Enumeration e = selectionListeners.elements();
            while (e.hasMoreElements()) {
                SelectionListener sl = (SelectionListener)e.nextElement();
                sl.selectionChanged(oldSelectedIndex, selectedIndex);
            }
        }
    }

    public void addDataChangedListener(DataChangedListener dl) {
        dataChangedListeners.addElement(dl);
    }

    public void removeDataChangedListener(DataChangedListener dl) {
        dataChangedListeners.removeElement(dl);
    }

    public void addSelectionListener(SelectionListener sl) {
        selectionListeners.addElement(sl);
    }

    public void removeSelectionListener(SelectionListener sl) {
        selectionListeners.removeElement(sl);
    }

    public void addItem(Object o) {
        itemStore.addElement(o);
        notifyDataChangedListeners(DataChangedListener.ADDED,
                itemStore.size() - 1);
    }

    public void removeItem(int i) {            
        itemStore.removeElementAt(i);
        notifyDataChangedListeners(DataChangedListener.REMOVED, i);
    }

    private void notifyDataChangedListeners(int type, int index) {
        Enumeration e = dataChangedListeners.elements();
        while (e.hasMoreElements()) {
            DataChangedListener dl = (DataChangedListener)e.nextElement();
            dl.dataChanged(type, index);
        }
    }
}

A List can be then created using a MyListModel instance:

// Initialize the custom list model and populate it with data
MyListModel myListModel = new MyListModel();
myListModel.addItem("Red");
myListModel.addItem("Blue");
myListModel.addItem("Green");
myListModel.addItem("Yellow");

// Creating a List with "myListModel"
List list = new List(myListModel);