Class List
- All Implemented Interfaces:
- ImageObserver,- ItemSelectable,- MenuContainer,- Serializable,- Accessible
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.
- Since:
- 1.0
- See Also:
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionprotected classThis class implements accessibility support for theListclass.Nested classes/interfaces declared in class java.awt.ComponentComponent.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
- 
Field SummaryFields declared in class java.awt.ComponentaccessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENTFields declared in interface java.awt.image.ImageObserverABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionvoidAdds the specified item to the end of scrolling list.voidAdds the specified item to the scrolling list at the position indicated by the index.voidAdds the specified action listener to receive action events from this list.voidDeprecated.voidDeprecated.replaced byadd(String, int).voidAdds the specified item listener to receive item events from this list.voidCreates the peer for the list.booleanDeprecated.As of JDK version 1.1, replaced byisMultipleMode().voidclear()Deprecated.As of JDK version 1.1, replaced byremoveAll().intDeprecated.As of JDK version 1.1, replaced bygetItemCount().voiddelItem(int position) Deprecated.replaced byremove(String)andremove(int).voiddelItems(int start, int end) Deprecated.As of JDK version 1.1, Not for public use in the future.voiddeselect(int index) Deselects the item at the specified index.Gets theAccessibleContextassociated with thisList.Returns an array of all the action listeners registered on this list.getItem(int index) Gets the item associated with the specified index.intGets the number of items in the list.Returns an array of all the item listeners registered on this list.String[]getItems()Gets the items in the list.<T extends EventListener>
 T[]getListeners(Class<T> listenerType) Returns an array of all the objects currently registered asFooListeners upon thisList.Determines the minimum size of this scrolling list.getMinimumSize(int rows) Gets the minimum dimensions for a list with the specified number of rows.Gets the preferred size of this scrolling list.getPreferredSize(int rows) Gets the preferred dimensions for a list with the specified number of rows.intgetRows()Gets the number of visible lines in this list.intGets the index of the selected item on the list,int[]Gets the selected indexes on the list.Gets the selected item on this scrolling list.String[]Gets the selected items on this scrolling list.Object[]Gets the selected items on this scrolling list in an array of Objects.intGets the index of the item that was last made visible by the methodmakeVisible.booleanisIndexSelected(int index) Determines if the specified item in this scrolling list is selected.booleanDetermines whether this list allows multiple selections.booleanisSelected(int index) Deprecated.As of JDK version 1.1, replaced byisIndexSelected(int).voidmakeVisible(int index) Makes the item at the specified index visible.Deprecated.As of JDK version 1.1, replaced bygetMinimumSize().minimumSize(int rows) Deprecated.As of JDK version 1.1, replaced bygetMinimumSize(int).protected StringReturns the parameter string representing the state of this scrolling list.Deprecated.As of JDK version 1.1, replaced bygetPreferredSize().preferredSize(int rows) Deprecated.As of JDK version 1.1, replaced bygetPreferredSize(int).protected voidProcesses action events occurring on this component by dispatching them to any registeredActionListenerobjects.protected voidProcesses events on this scrolling list.protected voidProcesses item events occurring on this list by dispatching them to any registeredItemListenerobjects.voidremove(int position) Removes the item at the specified position from this scrolling list.voidRemoves the first occurrence of an item from the list.voidRemoves the specified action listener so that it no longer receives action events from this list.voidRemoves all items from this list.voidRemoves the specified item listener so that it no longer receives item events from this list.voidRemoves the peer for this list.voidreplaceItem(String newValue, int index) Replaces the item at the specified index in the scrolling list with the new string.voidselect(int index) Selects the item at the specified index in the scrolling list.voidsetMultipleMode(boolean b) Sets the flag that determines whether this list allows multiple selections.voidsetMultipleSelections(boolean b) Deprecated.As of JDK version 1.1, replaced bysetMultipleMode(boolean).Methods declared in class java.awt.Componentaction, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, 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, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, revalidate, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setMixingCutoutShape, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, update, validate
- 
Constructor Details- 
ListCreates a new scrolling list. By default, there are four visible lines and multiple selections are not allowed. Note that this is a convenience method forList(0, false). Also note that the number of visible lines in the list cannot be changed after it has been created.- Throws:
- HeadlessException- if GraphicsEnvironment.isHeadless() returns true.
- See Also:
 
- 
ListCreates a new scrolling list initialized with the specified number of visible lines. By default, multiple selections are not allowed. Note that this is a convenience method forList(rows, false). Also note that the number of visible rows in the list cannot be changed after it has been created.- Parameters:
- rows- the number of items to show.
- Throws:
- HeadlessException- if GraphicsEnvironment.isHeadless() returns true.
- Since:
- 1.1
- See Also:
 
- 
ListCreates a new scrolling list initialized to display the specified number of rows. Note that if zero rows are specified, then the list will be created with a default of four rows. Also note that the number of visible rows in the list cannot be changed after it has been created. If the value ofmultipleModeistrue, then the user can select multiple items from the list. If it isfalse, 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.
- Throws:
- HeadlessException- if GraphicsEnvironment.isHeadless() returns true.
- See Also:
 
 
- 
- 
Method Details- 
addNotify
- 
removeNotifypublic void removeNotify()Removes the peer for this list. The peer allows us to modify the list's appearance without changing its functionality.- Overrides:
- removeNotifyin class- Component
- See Also:
 
- 
getItemCountpublic int getItemCount()Gets the number of items in the list.- Returns:
- the number of items in the list
- Since:
- 1.1
- See Also:
 
- 
countItemsDeprecated.As of JDK version 1.1, replaced bygetItemCount().Returns the number of items in the list.- Returns:
- the number of items in the list
 
- 
getItemGets 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:
 
- 
getItemsGets the items in the list.- Returns:
- a string array containing items of the list
- Since:
- 1.1
- See Also:
 
- 
addAdds the specified item to the end of scrolling list.- Parameters:
- item- the item to be added
- Since:
- 1.1
 
- 
addItemDeprecated.replaced byadd(String).Adds the specified item to the end of the list.- Parameters:
- item- the item to be added
 
- 
addAdds the specified item to 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- nullthen the item is treated as an empty string,- ""
- index- the position at which to add the item
- Since:
- 1.1
 
- 
addItemDeprecated.replaced byadd(String, int).Adds the specified item to the list at the position indicated by the index.- Parameters:
- item- the item to be added
- index- the position at which to add the item
 
- 
replaceItemReplaces 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
- Throws:
- ArrayIndexOutOfBoundsException- if- indexis out of range
 
- 
removeAllpublic void removeAll()Removes all items from this list.- Since:
- 1.1
- See Also:
 
- 
clearDeprecated.As of JDK version 1.1, replaced byremoveAll().
- 
removeRemoves the first occurrence of an item from the list. If the specified item is selected, and is the only selected item in the list, the list is set to have no selection.- Parameters:
- item- the item to remove from the list
- Throws:
- IllegalArgumentException- if the item doesn't exist in the list
- Since:
- 1.1
 
- 
removepublic void remove(int position) Removes the item at the specified position from this scrolling list. If the item with the specified position is selected, and is the only selected item in the list, the list is set to have no selection.- Parameters:
- position- the index of the item to delete
- Throws:
- ArrayIndexOutOfBoundsException- if the- positionis less than 0 or greater than- getItemCount()-1
- Since:
- 1.1
- See Also:
 
- 
delItemDeprecated.replaced byremove(String)andremove(int).Removes the item at the specified position.- Parameters:
- position- the index of the item to delete
 
- 
getSelectedIndexpublic int getSelectedIndex()Gets the index of the selected item on the list,- Returns:
- the index of the selected item;
                if no item is selected, or if multiple items are
                selected, -1is returned.
- See Also:
 
- 
getSelectedIndexespublic int[] getSelectedIndexes()Gets the selected indexes on the list.- Returns:
- an array of the selected indexes on this scrolling list; if no item is selected, a zero-length array is returned.
- See Also:
 
- 
getSelectedItemGets the selected item on this scrolling list.- Returns:
- the selected item on the list;
                if no item is selected, or if multiple items are
                selected, nullis returned.
- See Also:
 
- 
getSelectedItemsGets the selected items on this scrolling list.- Returns:
- an array of the selected items on this scrolling list; if no item is selected, a zero-length array is returned.
- See Also:
 
- 
getSelectedObjectsGets the selected items on this scrolling list in an array of Objects.- Specified by:
- getSelectedObjectsin interface- ItemSelectable
- Returns:
- an array of Objects representing the selected items on this scrolling list; if no item is selected, a zero-length array is returned.
- See Also:
 
- 
selectpublic void select(int index) Selects the item at the specified index in the scrolling list.Note that passing out of range parameters is invalid, and will result in unspecified behavior. Note that this method should be primarily used to initially select an item in this component. Programmatically calling this method will not trigger an ItemEvent. The only way to trigger anItemEventis by user interaction.- Parameters:
- index- the position of the item to select
- See Also:
 
- 
deselectpublic void deselect(int index) Deselects the item at the specified index.Note that passing out of range parameters is invalid, and will result in unspecified behavior. If the item at the specified index is not selected, then the operation is ignored. - Parameters:
- index- the position of the item to deselect
- See Also:
 
- 
isIndexSelectedpublic boolean isIndexSelected(int index) Determines if the specified item in this scrolling list is selected.- Parameters:
- index- the item to be checked
- Returns:
- trueif the specified item has been selected;- falseotherwise
- Since:
- 1.1
- See Also:
 
- 
isSelectedDeprecated.As of JDK version 1.1, replaced byisIndexSelected(int).Determines if the specified item in the list is selected.- Parameters:
- index- specifies the item to be checked
- Returns:
- trueif the item is selected; otherwise- false
 
- 
getRowspublic int getRows()Gets the number of visible lines in this list. Note that once theListhas been created, this number will never change.- Returns:
- the number of visible lines in this scrolling list
 
- 
isMultipleModepublic boolean isMultipleMode()Determines whether this list allows multiple selections.- Returns:
- trueif this list allows multiple selections; otherwise,- false
- Since:
- 1.1
- See Also:
 
- 
allowsMultipleSelectionsDeprecated.As of JDK version 1.1, replaced byisMultipleMode().Determines whether this list allows multiple selections.- Returns:
- trueif this list allows multiple selections; otherwise- false
 
- 
setMultipleModepublic void setMultipleMode(boolean b) Sets the flag that determines whether this list allows multiple selections. When the selection mode is changed from multiple-selection to single-selection, the selected items change as follows: If a selected item has the location cursor, only that item will remain selected. If no selected item has the location cursor, all items will be deselected.- Parameters:
- b- if- truethen multiple selections are allowed; otherwise, only one item from the list can be selected at once
- Since:
- 1.1
- See Also:
 
- 
setMultipleSelectionsDeprecated.As of JDK version 1.1, replaced bysetMultipleMode(boolean).Enables or disables multiple selection mode for this list.- Parameters:
- b-- trueto enable multiple mode,- falseotherwise
 
- 
getVisibleIndexpublic int getVisibleIndex()Gets the index of the item that was last made visible by the methodmakeVisible.- Returns:
- the index of the item that was last made visible
- See Also:
 
- 
makeVisiblepublic void makeVisible(int index) Makes the item at the specified index visible.- Parameters:
- index- the position of the item
- See Also:
 
- 
getPreferredSizeGets 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:
- 1.1
- See Also:
 
- 
preferredSizeDeprecated.As of JDK version 1.1, replaced bygetPreferredSize(int).Returns the preferred size of this component assuming it has the specified number of rows.- Parameters:
- rows- the number of rows
- Returns:
- the preferred dimensions for displaying this list
 
- 
getPreferredSizeGets the preferred size of this scrolling list.- Overrides:
- getPreferredSizein class- Component
- Returns:
- the preferred dimensions for displaying this scrolling list
- Since:
- 1.1
- See Also:
 
- 
preferredSizeDeprecated.As of JDK version 1.1, replaced bygetPreferredSize().Description copied from class:ComponentReturns the component's preferred size.- Overrides:
- preferredSizein class- Component
- Returns:
- the component's preferred size
 
- 
getMinimumSizeGets the minimum 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:
- 1.1
- See Also:
 
- 
minimumSizeDeprecated.As of JDK version 1.1, replaced bygetMinimumSize(int).Returns the minimum dimensions for the list with the specified number of rows.- Parameters:
- rows- the number of rows in the list
- Returns:
- the minimum dimensions for displaying this list
 
- 
getMinimumSizeDetermines the minimum size of this scrolling list.- Overrides:
- getMinimumSizein class- Component
- Returns:
- the minimum dimensions needed to display this scrolling list
- Since:
- 1.1
- See Also:
 
- 
minimumSizeDeprecated.As of JDK version 1.1, replaced bygetMinimumSize().Description copied from class:ComponentReturns the minimum size of this component.- Overrides:
- minimumSizein class- Component
- Returns:
- the minimum size of this component
 
- 
addItemListenerAdds the specified item listener to receive item events from this list. Item events are sent in response to user input, but not in response to calls toselectordeselect. If listenerlisnull, no exception is thrown and no action is performed.Refer to AWT Threading Issues for details on AWT's threading model. - Specified by:
- addItemListenerin interface- ItemSelectable
- Parameters:
- l- the item listener
- Since:
- 1.1
- See Also:
 
- 
removeItemListenerRemoves the specified item listener so that it no longer receives item events from this list. If listenerlisnull, no exception is thrown and no action is performed.Refer to AWT Threading Issues for details on AWT's threading model. - Specified by:
- removeItemListenerin interface- ItemSelectable
- Parameters:
- l- the item listener
- Since:
- 1.1
- See Also:
 
- 
getItemListenersReturns an array of all the item listeners registered on this list.- Returns:
- all of this list's ItemListeners or an empty array if no item listeners are currently registered
- Since:
- 1.4
- See Also:
 
- 
addActionListenerAdds the specified action listener to receive action events from this list. Action events occur when a user double-clicks on a list item or types Enter when the list has the keyboard focus.If listener lisnull, no exception is thrown and no action is performed.Refer to AWT Threading Issues for details on AWT's threading model. - Parameters:
- l- the action listener
- Since:
- 1.1
- See Also:
 
- 
removeActionListenerRemoves 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 listenerlisnull, no exception is thrown and no action is performed.Refer to AWT Threading Issues for details on AWT's threading model. - Parameters:
- l- the action listener
- Since:
- 1.1
- See Also:
 
- 
getActionListenersReturns an array of all the action listeners registered on this list.- Returns:
- all of this list's ActionListeners or an empty array if no action listeners are currently registered
- Since:
- 1.4
- See Also:
 
- 
getListenersReturns an array of all the objects currently registered asFooListeners upon thisList.FooListeners are registered using theaddFooListenermethod.You can specify the listenerTypeargument with a class literal, such asFooListener.class. For example, you can query aList lfor its item listeners with the following code:ItemListener[] ils = (ItemListener[])(l.getListeners(ItemListener.class)); If no such listeners exist, this method returns an empty array.- Overrides:
- getListenersin class- Component
- Type Parameters:
- T- the type of the listeners
- Parameters:
- listenerType- the type of listeners requested; this parameter should specify an interface that descends from- java.util.EventListener
- Returns:
- an array of all objects registered as
          FooListeners on this list, or an empty array if no such listeners have been added
- Throws:
- ClassCastException- if- listenerTypedoesn't specify a class or interface that implements- java.util.EventListener
- Since:
- 1.3
- See Also:
 
- 
processEventProcesses events on this scrolling list. If an event is an instance ofItemEvent, it invokes theprocessItemEventmethod. Else, if the event is an instance ofActionEvent, it invokesprocessActionEvent. If the event is not an item event or an action event, it invokesprocessEventon the superclass.Note that if the event parameter is nullthe behavior is unspecified and may result in an exception.- Overrides:
- processEventin class- Component
- Parameters:
- e- the event
- Since:
- 1.1
- See Also:
 
- 
processItemEventProcesses item events occurring on this list by dispatching them to any registeredItemListenerobjects.This method is not called unless item events are enabled for this component. Item events are enabled when one of the following occurs: - An ItemListenerobject is registered viaaddItemListener.
- Item events are enabled via enableEvents.
 Note that if the event parameter is nullthe behavior is unspecified and may result in an exception.- Parameters:
- e- the item event
- Since:
- 1.1
- See Also:
 
- An 
- 
processActionEventProcesses action events occurring on this component by dispatching them to any registeredActionListenerobjects.This method is not called unless action events are enabled for this component. Action events are enabled when one of the following occurs: - An ActionListenerobject is registered viaaddActionListener.
- Action events are enabled via enableEvents.
 Note that if the event parameter is nullthe behavior is unspecified and may result in an exception.- Parameters:
- e- the action event
- Since:
- 1.1
- See Also:
 
- An 
- 
paramStringReturns the parameter string representing the state of this scrolling list. This string is useful for debugging.- Overrides:
- paramStringin class- Component
- Returns:
- the parameter string of this scrolling list
 
- 
delItemsDeprecated.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.- Parameters:
- start- the beginning index of the range to delete
- end- the ending index of the range to delete
 
- 
getAccessibleContextGets theAccessibleContextassociated with thisList. For lists, theAccessibleContexttakes the form of anAccessibleAWTList. A newAccessibleAWTListinstance is created, if necessary.- Specified by:
- getAccessibleContextin interface- Accessible
- Overrides:
- getAccessibleContextin class- Component
- Returns:
- an AccessibleAWTListthat serves as theAccessibleContextof thisList
- Since:
- 1.3
 
 
- 
add(String).