Help Center

Local Navigation

Create a list box

Use a list box to display a list from which users can select one or more values.

  1. Import the following classes:
    import net.rim.device.api.system.*;
    import net.rim.device.api.ui.*;
    import net.rim.device.api.ui.component.*;
    import net.rim.device.api.ui.container.*;
    import java.util.Vector;
  2. Create the application framework by extending the UiApplication class. In main(), create an instance of the new class and invoke enterEventDispatcher(), to enable the application to receive events. In the constructor, invoke pushScreen, to display the custom screen for the application. The CreateMenuScreen class represents the custom screen which is described in step 3.
    public class ListFields extends UiApplication
    {
         public static void main(String[] args)
         {
             ListFields theApp = new ListFields();
             theApp.enterEventDispatcher();
         }
         public ListFields()
         {
             pushScreen(new ListFieldScreen());
         }
    } 
  3. Create the custom screen for the application by extending the MainScreen class. In the constructor, invoke setTitle(), to display the title for the screen. Invoke add(), to display a text field on the screen. Invoke addMenuItem(), to add a menu item to the menu that MainScreen creates.
    class ListFieldScreen extends MainScreen
    {
        private ListField _listField;
        private Vector _listElements;
    
        public ListFieldScreen() 
        {
            setTitle("List Field Sample");
        }
    }
  4. In the screen constructor, create the list box. Create an array for the items that you want to add to the list box by using the Vector class. Create the list box by using the ListField() class. Invoke add(), to add the list box to the screen. Invoke initializeList(), which is described in step 4, to add build the list box.
    _listElements = new Vector(); 
    _listField = new ListField();
    ListCallback _callback = new ListCallback();
    _listField.setCallback(_callback);
    add(_listField);
    initializeList();
  5. Create a method to specify the items that you want to appear in the list box by using the String object. Invoke addElement() to add the items to the list. Invoke setSize(), to specify the number of items in the list box.
    private void initializeList()
    {
        String itemOne = "List item one";
        String itemTwo = "List item two";
        _listElements.addElement(itemOne);
        _listElements.addElement(itemTwo);
        reloadList();
    }
    private void reloadList()
    {
        _listField.setSize(_listElements.size());
    }
  6. Create a class that implements the ListFieldCallback interface. Implement drawListRow(), to add the list box items to the screen. Implement get(), to return the list box item at the specified index. Implement indexOfList(), to return the first occurrence of a given string. Implement getPreferredWidth(), to retrieve the width of the list box.
    private class ListCallback implements ListFieldCallback 
    {
        public void drawListRow(ListField list, Graphics g, int index, int y, int w) 
        { 
            String text = (String)_listElements.elementAt(index); 
            g.drawText(text, 0, y, 0, w); 
        } 
        public Object get(ListField list, int index) 
        {
            return _listElements.elementAt(index); 
        } 
        public int indexOfList(ListField list, String prefix, int string) 
        { 
            return _listElements.indexOf(prefix, string); 
        } 
        public int getPreferredWidth(ListField list) 
        { 
            return Display.getWidth(); 
        } 
    }
Next topic: Radio buttons

Was this information helpful? Send us your comments.