java.awt
Class List

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.List
All Implemented Interfaces:
Accessible, ImageObserver, ItemSelectable, MenuContainer, Serializable

public class List
extends Component
implements ItemSelectable, Accessible

The List component presents the user with a scrolling list of text items. The list can be set up so that the user can choose either one item or multiple items.

For example, the code . . .


 List lst = new List(4, false);
 lst.add("Mercury");
 lst.add("Venus");
 lst.add("Earth");
 lst.add("JavaSoft");
 lst.add("Mars");
 lst.add("Jupiter");
 lst.add("Saturn");
 lst.add("Uranus");
 lst.add("Neptune");
 lst.add("Pluto");
 cnt.add(lst);
 

where cnt is a container, produces the following scrolling list:

Clicking on an item that isn't selected selects it. Clicking on an item that is already selected deselects it. In the preceding example, only one item from the scrolling list can be selected at a time, since the second argument when creating the new scrolling list is false. Selecting an item causes any other selected item to be automatically deselected.

Beginning with Java 1.1, the Abstract Window Toolkit sends the List object all mouse, keyboard, and focus events that occur over it. (The old AWT event model is being maintained only for backwards compatibility, and its use is discouraged.)

When an item is selected or deselected, AWT sends an instance of ItemEvent to the list. When the user double-clicks on an item in a scrolling list, AWT sends an instance of ActionEvent to the list following the item event. AWT also generates an action event when the user presses the return key while an item in the list is selected.

If an application wants to perform some action based on an item in this list being selected or activated, it should implement ItemListener or ActionListener as appropriate and register the new listener to receive events from this list.

For multiple-selection scrolling lists, it is considered a better user interface to use an external gesture (such as clicking on a button) to trigger the action.

Since:
JDK1.0
See Also:
ItemEvent, ItemListener, ActionEvent, ActionListener, Serialized Form

Inner Class Summary
protected  class List.AccessibleAWTList
List.AccessibleAWTList
This class implements accessibility support for the List class. It provides an implementation of the Java Accessibility API appropriate to list user-interface elements.
 
Inner classes inherited from class java.awt.Component
Component.AccessibleAWTComponent
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
List
copy->  new List()
copy->  <List var> = new List();
Creates a new scrolling list. By default, there are four visible lines and multiple selections are not allowed.
List
copy->  new List( )
copy->  <List var> = new List(<int rows>);
Creates a new scrolling list initialized with the specified number of visible lines. By default, multiple selections are not allowed.
Parameters:
rows - the number of items to show.
Since:
JDK1.1
List
copy->  new List(, )
copy->  <List var> = new List(<int rows>, <boolean multipleMode>);
Creates a new scrolling list initialized to display the specified number of rows. If the value of multipleMode is true, then the user can select multiple items from the list. If it is false, only one item at a time can be selected.
Parameters:
rows - the number of items to show.
multipleMode - if true, then multiple selections are allowed; otherwise, only one item can be selected at a time.
 
Method Summary
 void add(String item)
copy->  .add( )
copy->  <List>.add(<String item>);
Adds the specified item to the end of scrolling list.
Parameters:
item - the item to be added.
Since:
JDK1.1
 void add(String item, int index)
copy->  .add(, )
copy->  <List>.add(<String item>, <int index>);
Adds the specified item to the the scrolling list at the position indicated by the index. The index is zero-based. If the value of the index is less than zero, or if the value of the index is greater than or equal to the number of items in the list, then the item is added to the end of the list.
Parameters:
item - the item to be added. If this parameter is null then the item is treated as an empty string, "".
index - the position at which to add the item.
Since:
JDK1.1
 void addActionListener(ActionListener l)
copy->  .addActionListener( )
copy->  <List>.addActionListener(<ActionListener l>);
Adds the specified action listener to receive action events from this list. Action events occur when a user double-clicks on a list item. If l is null, no exception is thrown and no action is performed.
Parameters:
l - the action listener.
Since:
JDK1.1
See Also:
ActionEvent, ActionListener, removeActionListener(java.awt.event.ActionListener)
 void addItem(String item)
copy->  .addItem( )
copy->  <List>.addItem(<String item>);
Deprecated. replaced by add(String).

 void addItem(String item, int index)
copy->  .addItem(, )
copy->  <List>.addItem(<String item>, <int index>);
Deprecated. replaced by add(String, int).

 void addItemListener(ItemListener l)
copy->  .addItemListener( )
copy->  <List>.addItemListener(<ItemListener l>);
Adds the specified item listener to receive item events from this list. If l is null, no exception is thrown and no action is performed.
Specified by:
addItemListener in interface ItemSelectable
Parameters:
l - the item listener.
Since:
JDK1.1
See Also:
ItemEvent, ItemListener, removeItemListener(java.awt.event.ItemListener)
 void addNotify()
copy->  .addNotify()
copy->  <List>.addNotify();
Creates the peer for the list. The peer allows us to modify the list's appearance without changing its functionality.
Overrides:
addNotify in class Component
Following copied from class: java.awt.Component
See Also:
Component.isDisplayable(), Component.removeNotify()
 boolean allowsMultipleSelections()
copy->  .allowsMultipleSelections()
copy->  <boolean var>=<List>.allowsMultipleSelections();
Deprecated. As of JDK version 1.1, replaced by isMultipleMode().

 void clear()
copy->  .clear()
copy->  <List>.clear();
Deprecated. As of JDK version 1.1, replaced by removeAll().

 int countItems()
copy->  .countItems()
copy->  <int var>=<List>.countItems();
Deprecated. As of JDK version 1.1, replaced by getItemCount().

 void delItem(int position)
copy->  .delItem( )
copy->  <List>.delItem(<int position>);
Deprecated. replaced by remove(String) and remove(int).

 void delItems(int start, int end)
copy->  .delItems(, )
copy->  <List>.delItems(<int start>, <int end>);
Deprecated. As of JDK version 1.1, Not for public use in the future. This method is expected to be retained only as a package private method.

 void deselect(int index)
copy->  .deselect( )
copy->  <List>.deselect(<int index>);
Deselects the item at the specified index.

If the item at the specified index is not selected, or if the index is out of range, then the operation is ignored.

Parameters:
index - the position of the item to deselect.
See Also:
select(int), getSelectedItem(), isIndexSelected(int)
 AccessibleContext getAccessibleContext()
copy->  .getAccessibleContext()
copy->  <AccessibleContext var>=<List>.getAccessibleContext();
Gets the AccessibleContext associated with this List. For lists, the AccessibleContext takes the form of an AccessibleAWTList. A new AccessibleAWTList instance is created if necessary.
Specified by:
getAccessibleContext in interface Accessible
Overrides:
getAccessibleContext in class Component
Returns:
an AccessibleAWTList that serves as the AccessibleContext of this List
 String getItem(int index)
copy->  .getItem( )
copy->  <String var>=<List>.getItem(<int index>);
Gets the item associated with the specified index.
Parameters:
index - the position of the item.
Returns:
an item that is associated with the specified index.
See Also:
getItemCount()
 int getItemCount()
copy->  .getItemCount()
copy->  <int var>=<List>.getItemCount();
Gets the number of items in the list.
Returns:
the number of items in the list.
Since:
JDK1.1
See Also:
getItem(int)
 String[] getItems()
copy->  .getItems()
copy->  <String var>=<List>.getItems();
Gets the items in the list.
Returns:
a string array containing items of the list.
Since:
JDK1.1
See Also:
select(int), deselect(int), isIndexSelected(int)
 EventListener[] getListeners(Class listenerType)
copy->  .getListeners( )
copy->  <EventListener var>=<List>.getListeners(<Class listenerType>);
Return an array of all the listeners that were added to the List with addXXXListener(), where XXX is the name of the listenerType argument. For example, to get all of the ItemListener(s) for the given List l, one would write:
 ItemListener[] ils = (ItemListener[])(l.getListeners(ItemListener.class))
 
If no such listener list exists, then an empty array is returned.
Overrides:
getListeners in class Component
Parameters:
listenerType - Type of listeners requested
Returns:
all of the listeners of the specified type supported by this list
Since:
1.3
 Dimension getMinimumSize()
copy->  .getMinimumSize()
copy->  <Dimension var>=<List>.getMinimumSize();
Determines the minimum size of this scrolling list.
Overrides:
getMinimumSize in class Component
Returns:
the minimum dimensions needed to display this scrolling list.
Since:
JDK1.1
See Also:
Component.getMinimumSize()
 Dimension getMinimumSize(int rows)
copy->  .getMinimumSize( )
copy->  <Dimension var>=<List>.getMinimumSize(<int rows>);
Gets the minumum dimensions for a list with the specified number of rows.
Parameters:
rows - number of rows in the list.
Returns:
the minimum dimensions for displaying this scrolling list given that the specified number of rows must be visible.
Since:
JDK1.1
See Also:
Component.getMinimumSize()
 Dimension getPreferredSize()
copy->  .getPreferredSize()
copy->  <Dimension var>=<List>.getPreferredSize();
Gets the preferred size of this scrolling list.
Overrides:
getPreferredSize in class Component
Returns:
the preferred dimensions for displaying this scrolling list.
Since:
JDK1.1
See Also:
Component.getPreferredSize()
 Dimension getPreferredSize(int rows)
copy->  .getPreferredSize( )
copy->  <Dimension var>=<List>.getPreferredSize(<int rows>);
Gets the preferred dimensions for a list with the specified number of rows.
Parameters:
rows - number of rows in the list.
Returns:
the preferred dimensions for displaying this scrolling list given that the specified number of rows must be visible.
Since:
JDK1.1
See Also:
Component.getPreferredSize()
 int getRows()
copy->  .getRows()
copy->  <int var>=<List>.getRows();
Get the number of visible lines in this list.
Returns:
the number of visible lines in this scrolling list.
 int getSelectedIndex()
copy->  .getSelectedIndex()
copy->  <int var>=<List>.getSelectedIndex();
Gets the index of the selected item on the list,
Returns:
the index of the selected item, or -1 if no item is selected, or if more that one item is selected.
See Also:
select(int), deselect(int), isIndexSelected(int)
 int[] getSelectedIndexes()
copy->  .getSelectedIndexes()
copy->  <int var>=<List>.getSelectedIndexes();
Gets the selected indexes on the list.
Returns:
an array of the selected indexes of this scrolling list. If no items are selected, a zero-length array is returned.
See Also:
select(int), deselect(int), isIndexSelected(int)
 String getSelectedItem()
copy->  .getSelectedItem()
copy->  <String var>=<List>.getSelectedItem();
Get the selected item on this scrolling list.
Returns:
the selected item on the list, or null if no item is selected.
See Also:
select(int), deselect(int), isIndexSelected(int)
 String[] getSelectedItems()
copy->  .getSelectedItems()
copy->  <String var>=<List>.getSelectedItems();
Get the selected items on this scrolling list.
Returns:
an array of the selected items on this scrolling list.
See Also:
select(int), deselect(int), isIndexSelected(int)
 Object[] getSelectedObjects()
copy->  .getSelectedObjects()
copy->  <Object var>=<List>.getSelectedObjects();
Returns the selected items on the list in an array of Objects.
Specified by:
getSelectedObjects in interface ItemSelectable
See Also:
ItemSelectable
 int getVisibleIndex()
copy->  .getVisibleIndex()
copy->  <int var>=<List>.getVisibleIndex();
Gets the index of the item that was last made visible by the method makeVisible.
Returns:
the index of the item that was last made visible.
See Also:
makeVisible(int)
 boolean isIndexSelected(int index)
copy->  .isIndexSelected( )
copy->  <boolean var>=<List>.isIndexSelected(<int index>);
Determines if the specified item in this scrolling list is selected.
Parameters:
index - the item to be checked.
Returns:
true if the specified item has been selected; false otherwise.
Since:
JDK1.1
See Also:
select(int), deselect(int)
 boolean isMultipleMode()
copy->  .isMultipleMode()
copy->  <boolean var>=<List>.isMultipleMode();
Determines whether this list allows multiple selections.
Returns:
true if this list allows multiple selections; otherwise, false.
Since:
JDK1.1
See Also:
setMultipleMode(boolean)
 boolean isSelected(int index)
copy->  .isSelected( )
copy->  <boolean var>=<List>.isSelected(<int index>);
Deprecated. As of JDK version 1.1, replaced by isIndexSelected(int).

 void makeVisible(int index)
copy->  .makeVisible( )
copy->  <List>.makeVisible(<int index>);
Makes the item at the specified index visible.
Parameters:
index - the position of the item.
See Also:
getVisibleIndex()
 Dimension minimumSize()
copy->  .minimumSize()
copy->  <Dimension var>=<List>.minimumSize();
Deprecated. As of JDK version 1.1, replaced by getMinimumSize().

Overrides:
minimumSize in class Component
 Dimension minimumSize(int rows)
copy->  .minimumSize( )
copy->  <Dimension var>=<List>.minimumSize(<int rows>);
Deprecated. As of JDK version 1.1, replaced by getMinimumSize(int).

protected  String paramString()
copy->  .paramString()
copy->  <String var>=<List>.paramString();
Returns the parameter string representing the state of this scrolling list. This string is useful for debugging.
Overrides:
paramString in class Component
Returns:
the parameter string of this scrolling list.
 Dimension preferredSize()
copy->  .preferredSize()
copy->  <Dimension var>=<List>.preferredSize();
Deprecated. As of JDK version 1.1, replaced by getPreferredSize().

Overrides:
preferredSize in class Component
 Dimension preferredSize(int rows)
copy->  .preferredSize( )
copy->  <Dimension var>=<List>.preferredSize(<int rows>);
Deprecated. As of JDK version 1.1, replaced by getPreferredSize(int).

protected  void processActionEvent(ActionEvent e)
copy->  .processActionEvent( )
copy->  <List>.processActionEvent(<ActionEvent e>);
Processes action events occurring on this component by dispatching them to any registered ActionListener objects.

This method is not called unless action events are enabled for this component. Action events are enabled when one of the following occurs:

  • An ActionListener object is registered via addActionListener.
  • Action events are enabled via enableEvents.
Parameters:
e - the action event.
Since:
JDK1.1
See Also:
ActionEvent, ActionListener, addActionListener(java.awt.event.ActionListener), Component.enableEvents(long)
protected  void processEvent(AWTEvent e)
copy->  .processEvent( )
copy->  <List>.processEvent(<AWTEvent e>);
Processes events on this scrolling list. If an event is an instance of ItemEvent, it invokes the processItemEvent method. Else, if the event is an instance of ActionEvent, it invokes processActionEvent. If the event is not an item event or an action event, it invokes processEvent on the superclass.
Overrides:
processEvent in class Component
Parameters:
e - the event.
Since:
JDK1.1
See Also:
ActionEvent, ItemEvent, processActionEvent(java.awt.event.ActionEvent), processItemEvent(java.awt.event.ItemEvent)
protected  void processItemEvent(ItemEvent e)
copy->  .processItemEvent( )
copy->  <List>.processItemEvent(<ItemEvent e>);
Processes item events occurring on this list by dispatching them to any registered ItemListener objects.

This method is not called unless item events are enabled for this component. Item events are enabled when one of the following occurs:

  • An ItemListener object is registered via addItemListener.
  • Item events are enabled via enableEvents.
Parameters:
e - the item event.
Since:
JDK1.1
See Also:
ItemEvent, ItemListener, addItemListener(java.awt.event.ItemListener), Component.enableEvents(long)
 void remove(int position)
copy->  .remove( )
copy->  <List>.remove(<int position>);
Remove the item at the specified position from this scrolling list.
Parameters:
position - the index of the item to delete.
Throws:
ArrayIndexOutOfBoundsException - if the position is less than 0 or greater than getItemCount()-1
Since:
JDK1.1
See Also:
add(String, int)
 void remove(String item)
copy->  .remove( )
copy->  <List>.remove(<String item>);
Removes the first occurrence of an item from the list.
Parameters:
item - the item to remove from the list.
Throws:
IllegalArgumentException - if the item doesn't exist in the list.
Since:
JDK1.1
 void removeActionListener(ActionListener l)
copy->  .removeActionListener( )
copy->  <List>.removeActionListener(<ActionListener l>);
Removes the specified action listener so that it no longer receives action events from this list. Action events occur when a user double-clicks on a list item. If l is null, no exception is thrown and no action is performed.
Parameters:
l - the action listener.
Since:
JDK1.1
See Also:
ActionEvent, ActionListener, addActionListener(java.awt.event.ActionListener)
 void removeAll()
copy->  .removeAll()
copy->  <List>.removeAll();
Removes all items from this list.
Since:
JDK1.1
See Also:
remove(java.lang.String), delItems(int, int)
 void removeItemListener(ItemListener l)
copy->  .removeItemListener( )
copy->  <List>.removeItemListener(<ItemListener l>);
Removes the specified item listener so that it no longer receives item events from this list. If l is null, no exception is thrown and no action is performed.
Specified by:
removeItemListener in interface ItemSelectable
Parameters:
l - the item listener.
Since:
JDK1.1
See Also:
ItemEvent, ItemListener, addItemListener(java.awt.event.ItemListener)
 void removeNotify()
copy->  .removeNotify()
copy->  <List>.removeNotify();
Removes the peer for this list. The peer allows us to modify the list's appearance without changing its functionality.
Overrides:
removeNotify in class Component
Following copied from class: java.awt.Component
See Also:
Component.isDisplayable(), Component.addNotify()
 void replaceItem(String newValue, int index)
copy->  .replaceItem(, )
copy->  <List>.replaceItem(<String newValue>, <int index>);
Replaces the item at the specified index in the scrolling list with the new string.
Parameters:
newValue - a new string to replace an existing item.
index - the position of the item to replace.
 void select(int index)
copy->  .select( )
copy->  <List>.select(<int index>);
Selects the item at the specified index in the scrolling list.
Parameters:
index - the position of the item to select.
See Also:
getSelectedItem(), deselect(int), isIndexSelected(int)
 void setMultipleMode(boolean b)
copy->  .setMultipleMode( )
copy->  <List>.setMultipleMode(<boolean b>);
Sets the flag that determines whether this list allows multiple selections.
Parameters:
b - if true then multiple selections are allowed; otherwise, only one item from the list can be selected at once.
Since:
JDK1.1
See Also:
isMultipleMode()
 void setMultipleSelections(boolean b)
copy->  .setMultipleSelections( )
copy->  <List>.setMultipleSelections(<boolean b>);
Deprecated. As of JDK version 1.1, replaced by setMultipleMode(boolean).

 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addPropertyChangeListener, addPropertyChangeListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentOrientation, getCursor, getDropTarget, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getInputContext, getInputMethodRequests, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getName, getParent, getPeer, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isDisplayable, isDoubleBuffered, isEnabled, isFocusTraversable, isLightweight, isOpaque, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paint, paintAll, postEvent, prepareImage, prepareImage, print, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFont, setForeground, setLocale, setLocation, setLocation, setName, setSize, setSize, setVisible, show, show, size, toString, transferFocus, update, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait