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:
If the List allows multiple selections, then 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. If the List does not allow multiple
selections, selecting an item causes any other selected item
to be deselected.
Note that the list in the example shown was created with four visible
rows. Once the list has been created, the number of visible rows
cannot be changed. A default List is created with
four rows, so that lst = new List() is equivalent to
list = new List(4, false).
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 by the user, 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 by the user, 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.
java.awt.event.ItemEvent , java.awt.event.ItemListener , java.awt.event.ActionEvent , java.awt.event.ActionListener
| Modifier and Type | Class and Description |
|---|---|
| protected class | List.
This class implements accessibility support for the
|
| Modifier and Type | Field and Description |
|---|---|
| pack-priv transient ActionListener | |
| private static final String | |
| pack-priv static final int | DEFAULT_VISIBLE_ROWS
The default number of visible rows is 4. |
| pack-priv transient ItemListener | |
| pack-priv Vector | items
A vector created to contain items which will become part of the List Component. |
| private int | listSerializedDataVersion
The |
| pack-priv boolean | multipleMode
|
| private static int | |
| pack-priv int | rows
This field will represent the number of visible rows in the
|
| pack-priv int[] | selected
|
| private static final long | serialVersionUID
Use serialVersionUID from JDK 1.1 for interoperability.
|
| pack-priv int | visibleIndex
This variable contains the value that will be used when trying to make a particular list item visible. |
| Access | Constructor and Description |
|---|---|
| public | |
| public | List(int
the number of items to show. rows)Creates a new scrolling list initialized with the specified number of visible lines. |
| public | List(int
the number of items to show. rows, boolean if multipleMode)true,
then multiple selections are allowed;
otherwise, only one item can be selected at a time.Creates a new scrolling list initialized to display the specified number of rows. |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public void | |
| public synchronized void | addActionListener(ActionListener
the action listener l)Adds the specified action listener to receive action events from this list. |
| public void | |
| public synchronized void | |
| public synchronized void | addItemListener(ItemListener
the item listener l)Implements java. Adds the specified item listener to receive item events from this list. |
| public void | |
| public boolean | Returns: true if this list allows multiple
selections; otherwise false
Deprecated
As of JDK version 1.1,
replaced by
isMultipleMode().
Determines whether this list allows multiple selections. |
| public synchronized void | |
| pack-priv String | constructComponentName()
Overrides java. Construct a name for this component. |
| public int | Returns: the number of items in the list
Deprecated
As of JDK version 1.1,
replaced by
getItemCount().
Returns the number of items in the list. |
| public void | delItem(int
the index of the item to delete position)
Deprecated
replaced by
remove(String)
and remove(int).
Removes the item at the specified position. |
| public synchronized void | delItems(int
the beginning index of the range to delete start, int the ending index of the range to delete 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.
Deletes the list items in the specified index range. |
| public synchronized void | |
| pack-priv boolean | |
| public AccessibleContext | Returns: anAccessibleAWTList that serves as the
AccessibleContext of this ListOverrides java. Implements javax. Gets the |
| public synchronized ActionListener[] | Returns: all of this list'sActionListeners
or an empty array if no action
listeners are currently registeredReturns an array of all the action listeners registered on this list. |
| public String | Returns: an item that is associated with the specified indexthe position of the item index)Gets the item associated with the specified index. |
| public int | |
| pack-priv final String | |
| public synchronized ItemListener[] | Returns: all of this list'sItemListeners
or an empty array if no item
listeners are currently registeredReturns an array of all the item listeners registered on this list. |
| public synchronized String[] | |
| public <T extends EventListener> T[] | Returns: an array of all objects registered asFooListeners on this list,
or an empty array if no such
listeners have been addedthe type of listeners requested; this parameter
should specify an interface that descends from
listenerType)java.util.EventListenerOverrides java. Returns an array of all the objects currently registered
as |
| public Dimension | Returns: the minimum dimensions for displaying this scrolling list given that the specified number of rows must be visiblenumber of rows in the list rows)Gets the minimum dimensions for a list with the specified number of rows. |
| public Dimension | Returns: the minimum dimensions needed to display this scrolling listOverrides java. Determines the minimum size of this scrolling list. |
| public Dimension | Returns: the preferred dimensions for displaying this scrolling list given that the specified number of rows must be visiblenumber of rows in the list rows)Gets the preferred dimensions for a list with the specified number of rows. |
| public Dimension | Returns: the preferred dimensions for displaying this scrolling listOverrides java. Gets the preferred size of this scrolling list. |
| public int | Returns: the number of visible lines in this scrolling listGets the number of visible lines in this list. |
| public synchronized int | Returns: the index of the selected item; if no item is selected, or if multiple items are selected,-1 is returned.Gets the index of the selected item on the list, |
| public synchronized int[] | Returns: an array of the selected indexes on this scrolling list; if no item is selected, a zero-length array is returned.Gets the selected indexes on the list. |
| public synchronized String | Returns: the selected item on the list; if no item is selected, or if multiple items are selected,null is returned.Gets the selected item on this scrolling list. |
| public synchronized String[] | Returns: an array of the selected items on this scrolling list; if no item is selected, a zero-length array is returned.Gets the selected items on this scrolling list. |
| public Object[] | Returns: an array ofObjects representing the
selected items on this scrolling list;
if no item is selected, a zero-length array is returned.Implements java. Gets the selected items on this scrolling list in an array of Objects. |
| public int | Returns: the index of the item that was last made visibleGets the index of the item that was last made visible by
the method |
| public boolean | Returns: true if the specified item has been
selected; false otherwisethe item to be checked index)Determines if the specified item in this scrolling list is selected. |
| public boolean | Returns: true if this list allows multiple
selections; otherwise, falseDetermines whether this list allows multiple selections. |
| public boolean | Returns: true if the item is selected; otherwise falsespecifies the item to be checked index)
Deprecated
As of JDK version 1.1,
replaced by
isIndexSelected(int).
Determines if the specified item in the list is selected. |
| public synchronized void | |
| public Dimension | Returns: the minimum dimensions for displaying this listthe number of rows in the list rows)
Deprecated
As of JDK version 1.1,
replaced by
getMinimumSize(int).
Returns the minimum dimensions for the list with the specified number of rows. |
| public Dimension | minimumSize()
Overrides java.
Deprecated
As of JDK version 1.1,
replaced by
getMinimumSize().
Returns the minimum size of this component. |
| protected String | Returns: the parameter string of this scrolling listOverrides java. Returns the parameter string representing the state of this scrolling list. |
| public Dimension | Returns: the preferred dimensions for displaying this listthe number of rows rows)
Deprecated
As of JDK version 1.1,
replaced by
getPreferredSize(int).
Returns the preferred size of this component assuming it has the specified number of rows. |
| public Dimension | preferredSize()
Overrides java.
Deprecated
As of JDK version 1.1,
replaced by
getPreferredSize().
Returns the component's preferred size. |
| protected void | processActionEvent(ActionEvent
the action event e)Processes action events occurring on this component
by dispatching them to any registered
|
| protected void | processEvent(AWTEvent
the event e)Overrides java. Processes events on this scrolling list. |
| protected void | processItemEvent(ItemEvent
the item event e)Processes item events occurring on this list by
dispatching them to any registered
|
| private void | readObject(ObjectInputStream
the s)ObjectInputStream to readHides java. Reads the |
| public synchronized void | |
| public void | remove(int
the index of the item to delete position)Removes the item at the specified position from this scrolling list. |
| public synchronized void | removeActionListener(ActionListener
the action listener l)Removes the specified action listener so that it no longer receives action events from this list. |
| public void | |
| public synchronized void | removeItemListener(ItemListener
the item listener l)Implements java. Removes the specified item listener so that it no longer receives item events from this list. |
| public void | |
| public synchronized void | replaceItem(String
a new string to replace an existing item newValue, int the position of the item to replace index)Replaces the item at the specified index in the scrolling list with the new string. |
| public void | select(int
the position of the item to select index)Selects the item at the specified index in the scrolling list. |
| public void | setMultipleMode(boolean
if b)true then multiple selections
are allowed; otherwise, only one item from
the list can be selected at onceSets the flag that determines whether this list allows multiple selections. |
| public synchronized void | setMultipleSelections(boolean
true to enable multiple mode, false otherwise
Deprecated
As of JDK version 1.1,
replaced by
setMultipleMode(boolean).
Enables or disables multiple selection mode for this list. |
| private void | writeObject(ObjectOutputStream
the s)ObjectOutputStream to writeHides java. Writes default serializable fields to stream. |