APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.awt

public Class List

extends Component
implements ItemSelectable, Accessible
Class Inheritance
All Implemented Interfaces
javax.accessibility.Accessible, java.awt.ItemSelectable
Imports
java.awt.event.ActionEvent, .ActionListener, .FocusListener, .ItemEvent, .ItemListener, java.awt.peer.ListPeer, java.io.IOException, .ObjectInputStream, .ObjectOutputStream, .Serial, java.util.EventListener, .Locale, .Vector, javax.accessibility.Accessible, .AccessibleContext, .AccessibleRole, .AccessibleSelection, .AccessibleState, .AccessibleStateSet

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:

Shows a list containing: Venus, Earth, JavaSoft, and Mars. Javasoft is
selected.

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.

Author
Sami Shaio
Since
1.0
See Also
java.awt.event.ItemEvent, java.awt.event.ItemListener, java.awt.event.ActionEvent, java.awt.event.ActionListener

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
List.AccessibleAWTList

This class implements accessibility support for the List class.

Field Summary

Modifier and TypeField 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<String>
items

A vector created to contain items which will become part of the List Component.

private int
listSerializedDataVersion

The List component's Serialized Data Version.

pack-priv boolean
multipleMode

multipleMode is a variable that will be set to true if a list component is to be set to multiple selection mode, that is where the user can select more than one item in a list at one time.

private static int
pack-priv int
rows

This field will represent the number of visible rows in the List Component.

pack-priv int[]
selected

selected is an array that will contain the indices of items that have been selected.

private static final long
serialVersionUID

Hides java.awt.Component.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.

Inherited from java.awt.Component:
accessibleContextactionListenerKadjustmentListenerKbackgroundbackgroundEraseDisabledBOTTOM_ALIGNMENTCENTER_ALIGNMENTcomponentListenercomponentListenerKcomponentOrientationcontainerListenerKcursordropTargetenabledeventCacheeventMaskfocusListenerfocusListenerKfocusTraversalKeysfontforegroundheighthierarchyBoundsListenerhierarchyBoundsListenerKhierarchyListenerhierarchyListenerKignoreRepaintincRateinputMethodListenerinputMethodListenerKisIncisPackeditemListenerKkeyListenerkeyListenerKLEFT_ALIGNMENTlocaleLOCKmaxSizemaxSizeSetminSizeminSizeSetmouseListenermouseListenerKmouseMotionListenermouseMotionListenerKmouseWheelListenermouseWheelListenerKnewEventsOnlyownedWindowKparentpeerpeerFontpopupsprefSizeprefSizeSetRIGHT_ALIGNMENTtextListenerKTOP_ALIGNMENTvisiblewidthwindowFocusListenerKwindowListenerKwindowStateListenerKxy

Constructor Summary

AccessConstructor and Description
public
List()

Creates a new scrolling list.

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 true, then multiple selections are allowed; otherwise, only one item can be selected at a time.
multipleMode
)

Creates a new scrolling list initialized to display the specified number of rows.

Method Summary

Modifier and TypeMethod and Description
public void
add(String
the item to be added
item
)

Adds the specified item to the end of scrolling list.

public void
add(String
the item to be added; if this parameter is null then the item is treated as an empty string, ""
item
,
int
the position at which to add the item
index
)

Adds the specified item to the scrolling list at the position indicated by the index.

public synchronized void
addActionListener(ActionListener
the action listener
l
)

Adds the specified action listener to receive action events from this list.

public void
addItem(String
the item to be added
item
)
Deprecated replaced by add(String).

Adds the specified item to the end of the list.

public synchronized void
addItem(String
the item to be added
item
,
int
the position at which to add the item
index
)
Deprecated replaced by add(String, int).

Adds the specified item to the list at the position indicated by the index.

public synchronized void
addItemListener(ItemListener
the item listener
l
)

Implements java.awt.ItemSelectable.addItemListener.

Adds the specified item listener to receive item events from this list.

public void
addNotify()

Overrides java.awt.Component.addNotify.

Creates the peer for the list.

public boolean

Returns:

true if this list allows multiple selections; otherwise false
allowsMultipleSelections
()
Deprecated As of JDK version 1.1, replaced by isMultipleMode().

Determines whether this list allows multiple selections.

public synchronized void
clear()
Deprecated As of JDK version 1.1, replaced by removeAll().

pack-priv String
constructComponentName()

Overrides java.awt.Component.constructComponentName.

Construct a name for this component.

public int

Returns:

the number of items in the list
countItems
()
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
deselect(int
the position of the item to deselect
index
)

Deselects the item at the specified index.

pack-priv boolean
public AccessibleContext

Returns:

an AccessibleAWTList that serves as the AccessibleContext of this List
getAccessibleContext
()

Overrides java.awt.Component.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

Gets the AccessibleContext associated with this List.

public synchronized ActionListener[]

Returns:

all of this list's ActionListeners or an empty array if no action listeners are currently registered
getActionListeners
()

Returns an array of all the action listeners registered on this list.

public String

Returns:

an item that is associated with the specified index
getItem
(int
the position of the item
index
)

Gets the item associated with the specified index.

public int

Returns:

the number of items in the list
getItemCount
()

Gets the number of items in the list.

pack-priv final String
getItemImpl(int index)

public synchronized ItemListener[]

Returns:

all of this list's ItemListeners or an empty array if no item listeners are currently registered
getItemListeners
()

Returns an array of all the item listeners registered on this list.

public synchronized String[]

Returns:

a string array containing items of the list
getItems
()

Gets the items in the list.

public <T extends EventListener> T[]

Returns:

an array of all objects registered as FooListeners on this list, or an empty array if no such listeners have been added
getListeners
(Class<T>
the type of listeners requested; this parameter should specify an interface that descends from java.util.EventListener
listenerType
)

Overrides java.awt.Component.getListeners.

Returns an array of all the objects currently registered as FooListeners upon this List.

public Dimension

Returns:

the minimum dimensions for displaying this scrolling list given that the specified number of rows must be visible
getMinimumSize
(int
number 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 list
getMinimumSize
()

Overrides java.awt.Component.getMinimumSize.

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 visible
getPreferredSize
(int
number 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 list
getPreferredSize
()

Overrides java.awt.Component.getPreferredSize.

Gets the preferred size of this scrolling list.

public int

Returns:

the number of visible lines in this scrolling list
getRows
()

Gets 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.
getSelectedIndex
()

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.
getSelectedIndexes
()

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.
getSelectedItem
()

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.
getSelectedItems
()

Gets the selected items on this scrolling list.

public Object[]

Returns:

an array of Objects representing the selected items on this scrolling list; if no item is selected, a zero-length array is returned.
getSelectedObjects
()

Implements java.awt.ItemSelectable.getSelectedObjects.

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 visible
getVisibleIndex
()

Gets the index of the item that was last made visible by the method makeVisible.

public boolean

Returns:

true if the specified item has been selected; false otherwise
isIndexSelected
(int
the 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, false
isMultipleMode
()

Determines whether this list allows multiple selections.

public boolean

Returns:

true if the item is selected; otherwise false
isSelected
(int
specifies 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
makeVisible(int
the position of the item
index
)

Makes the item at the specified index visible.

public Dimension

Returns:

the minimum dimensions for displaying this list
minimumSize
(int
the 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.awt.Component.minimumSize.

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 list
paramString
()

Overrides java.awt.Component.paramString.

Returns the parameter string representing the state of this scrolling list.

public Dimension

Returns:

the preferred dimensions for displaying this list
preferredSize
(int
the 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.awt.Component.preferredSize.

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 ActionListener objects.

protected void
processEvent(AWTEvent
the event
e
)

Overrides java.awt.Component.processEvent.

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 ItemListener objects.

private void
readObject(ObjectInputStream
the ObjectInputStream to read
s
)

Hides java.awt.Component.readObject.

Reads the ObjectInputStream and if it isn't null adds a listener to receive both item events and action events (as specified by the key stored in the stream) fired by the List.

public synchronized void
remove(String
the item to remove from the list
item
)

Removes the first occurrence of an item from the list.

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
removeAll()

Removes all items from this list.

public synchronized void
removeItemListener(ItemListener
the item listener
l
)

Implements java.awt.ItemSelectable.removeItemListener.

Removes the specified item listener so that it no longer receives item events from this list.

public void
removeNotify()

Overrides java.awt.Component.removeNotify.

Removes the peer for this list.

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 true then multiple selections are allowed; otherwise, only one item from the list can be selected at once
b
)

Sets the flag that determines whether this list allows multiple selections.

public synchronized void
setMultipleSelections(boolean
true to enable multiple mode, false otherwise
b
)
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 ObjectOutputStream to write
s
)

Hides java.awt.Component.writeObject.

Writes default serializable fields to stream.

Inherited from java.awt.Component:
actionaddaddComponentListeneraddFocusListeneraddHierarchyBoundsListeneraddHierarchyListeneraddInputMethodListeneraddKeyListeneraddMouseListeneraddMouseMotionListeneraddMouseWheelListeneraddPropertyChangeListeneraddPropertyChangeListeneradjustListeningChildrenOnParentapplyComponentOrientationapplyCompoundShapeapplyCurrentShapeareBoundsValidareFocusTraversalKeysSetareInputMethodsEnabledautoProcessMouseWheelboundscanBeFocusOwnercanBeFocusOwnerRecursivelycheckGDcheckImagecheckImagecheckTreeLockclearCurrentFocusCycleRootOnHideclearMostRecentFocusOwnerOnHidecoalesceEventscontainscontainscontainsFocuscountHierarchyMemberscreateBufferStrategycreateBufferStrategycreateHierarchyEventscreateImagecreateImagecreateVolatileImagecreateVolatileImagedeliverEventdisabledisableEventsdispatchEventdispatchEventImpldispatchMouseWheelToAncestordoLayoutenableenableenableEventsenableInputMethodseventTypeEnabledfindUnderMouseInWindowfirePropertyChangefirePropertyChangefirePropertyChangefirePropertyChangefirePropertyChangefirePropertyChangefirePropertyChangefirePropertyChangefirePropertyChangegetAccessibleIndexInParentgetAccessibleStateSetgetAlignmentXgetAlignmentYgetBackBuffergetBackgroundgetBaselinegetBaselineResizeBehaviorgetBoundsgetBoundsgetBoundsOpgetBufferStrategygetColorModelgetComponentAtgetComponentAtgetComponentFactorygetComponentListenersgetComponentOrientationgetContainergetContainingWindowgetCursorgetCursor_NoClientCodegetDropTargetgetFocusCycleRootAncestorgetFocusListenersgetFocusTraversalKeysgetFocusTraversalKeys_NoIDCheckgetFocusTraversalKeysEnabledgetFontgetFont_NoClientCodegetFontMetricsgetForegroundgetGraphicsgetGraphics_NoClientCodegetGraphicsConfigurationgetGraphicsConfiguration_NoClientCodegetHeightgetHierarchyBoundsListenersgetHierarchyListenersgetHWPeerAboveMegetIgnoreRepaintgetInputContextgetInputMethodListenersgetInputMethodRequestsgetKeyListenersgetLocalegetLocationgetLocationgetLocationOnScreengetLocationOnScreen_NoTreeLockgetLocationOnWindowgetMaximumSizegetMouseListenersgetMouseMotionListenersgetMousePositiongetMouseWheelListenersgetNamegetNativeContainergetNextFocusCandidategetNormalShapegetObjectLockgetOpaqueShapegetParentgetParent_NoClientCodegetPropertyChangeListenersgetPropertyChangeListenersgetSiblingIndexAbovegetSiblingIndexBelowgetSizegetSizegetToolkitgetToolkitImplgetTraversalRootgetTreeLockgetWidthgetXgetYgotFocushandleEventhasFocushideimageUpdateinitializeFocusTraversalKeysinsideinvalidateinvalidateIfValidinvalidateParentisAutoFocusTransferOnDisposalisBackgroundSetisCoalescingEnabledisCursorSetisDisplayableisDoubleBufferedisEnabledisEnabledImplisFocusableisFocusCycleRootisFocusOwnerisFocusTraversableisFocusTraversableOverriddenisFontSetisForegroundSetisInstanceOfisLightweightisMaximumSizeSetisMinimumSizeSetisMixingNeededisNonOpaqueForMixingisOpaqueisPreferredSizeSetisRecursivelyVisibleisSameOrAncestorOfisShowingisValidisVisibleisVisible_NoClientCodekeyDownkeyUplayoutlightweightPaintlightweightPrintlistlistlistlistlistlocatelocationlostFocusmixOnHidingmixOnReshapingmixOnShowingmixOnValidatingmixOnZOrderChangingmouseDownmouseDragmouseEntermouseExitmouseMovemouseUpmovenextFocusnumListeningpaintpaintAllpaintHeavyweightComponentspointRelativeToComponentpostEventpostsOldMouseEventsprepareImageprepareImageprintprintAllprintHeavyweightComponentsprocessComponentEventprocessFocusEventprocessHierarchyBoundsEventprocessHierarchyEventprocessInputMethodEventprocessKeyEventprocessMouseEventprocessMouseMotionEventprocessMouseWheelEventrelocateComponentremoveremoveComponentListenerremoveFocusListenerremoveHierarchyBoundsListenerremoveHierarchyListenerremoveInputMethodListenerremoveKeyListenerremoveMouseListenerremoveMouseMotionListenerremoveMouseWheelListenerremovePropertyChangeListenerremovePropertyChangeListenerrepaintrepaintrepaintrepaintrequestFocusrequestFocusrequestFocusrequestFocusrequestFocusHelperrequestFocusHelperrequestFocusInWindowrequestFocusInWindowrequestFocusInWindowrequestFocusInWindowreshaperesizeresizerevalidaterevalidateSynchronouslysetAutoFocusTransferOnDisposalsetBackgroundsetBoundssetBoundssetBoundsOpsetComponentOrientationsetCursorsetDropTargetsetEnabledsetFocusablesetFocusTraversalKeyssetFocusTraversalKeys_NoIDChecksetFocusTraversalKeysEnabledsetFontsetForegroundsetGraphicsConfigurationsetIgnoreRepaintsetLocalesetLocationsetLocationsetMaximumSizesetMinimumSizesetMixingCutoutShapesetNamesetPreferredSizesetRequestFocusControllersetSizesetSizesetVisibleshowshowsizesubtractAndApplyShapesubtractAndApplyShapeBelowMetoStringtransferFocustransferFocustransferFocusBackwardtransferFocusBackwardtransferFocusUpCycleupdateupdateChildGraphicsDataupdateCursorImmediatelyupdateGraphicsDataupdateZOrdervalidate