List Methods

You create a new List with one of these three constructors:

  public List()
  public List(int numLines)
  public List(int numLines, boolean allowMultipleSelections)

For example,

List l = new List(8, true);

numLines is the number of items you want to be visible in the scrolling list. It is not necessarily the same as the number of items in the list which is limited only by available memory. allowMultipleSelections says whether the user is allowed to select more than one item at once (typically by Shift-clicking).

The following methods add items at the end of the list:

  public void add(String item)
  public void addItem(String item)

These two methods add items at the specified position in the list.

  public void add(String item, int index)
  public void addItem(String item, int index)

The following methods remove items from the List:

  public void removeAll()
  public void remove(String item)
  public void remove(int position)
  public void delItem(int position)
These methods allow you to retrive particular items from the List:
  public int getItemCount()
  public String getItem(int index)
  public String[] getItems()

You ran also replace a particular item:

 public void replaceItem(String newValue, int index)

These methods allow you to determine which item the user has selected:

  public int      getSelectedIndex()
  public int[]    getSelectedIndexes()
  public String   getSelectedItem()
  public String[] getSelectedItems()
  public Object[] getSelectedObjects()

If a list allows multiple selections, you should use the plural forms of the above methods. You can determine whether multiple selections are allowed with isMultipleMode() and change it with setMultipleMode().

  public boolean isMultipleMode()
  public void    setMultipleMode(boolean b)

These methods allow you to manipulate the selection:

  public void    select(int index)
  public void    deselect(int index)
  public boolean isIndexSelected(int index)

These two methods determine whether an item at a particular index is currently visible in the List box:

  public int  getVisibleIndex()
  public void makeVisible(int index)

Previous | Next | Top | Cafe au Lait

Copyright 1997, 1999 Elliotte Rusty Harold
Last Modified November 15, 1999