Java Reference
In-Depth Information
Lists
A list component represents a list of items from which a user can select one or
more of the items. The java.awt.List class represents an AWT list, which can
only contain text entries. The javax.swing.JList class represents a Swing list,
which can contain text, images, and any other Object type. Let's look at both
classes.
AWT Lists
The java.awt.List class has three constructors:
public List(). Creates a new, empty single-selection list.
public List(int rows). Creates an empty single-selection list with the
specified numbered of rows visible.
public List(int rows, boolean multipleMode).
If the boolean value is
true, multiple items can be selected.
Some of the methods in the List class include:
public void add(String item).
Adds the specified item to the end of the
list.
public void add(String item, int index). Adds the item to the list at the
specified position, where 0 is the first position in the list.
public void addActionListener(ActionListener a). Adds the ActionLis-
tener to the list. An ActionEvent occurs when the use double-clicks on
an item in the list.
public void addItemListener(ItemListener i). Adds the ItemListener to
the list. An ItemEvent occurs when the user clicks on an item in the list.
public void setMultipleMode(boolean multiple).
Passes in true to
allow multiple selection in the list.
public String getSelectedItem().
Returns the currently selected item of
the list.
public String [] getSelectedItems(). Returns the currently selected items.
Use this method when the list allows multiple selection.
public void remove(int position).
Removes the item at the specified
position.
public void remove(String item).
Removes the first occurrence of the
given item.
public void select(int index).
Selects the specified item in the list.
Search WWH ::




Custom Search