Save This Page
Home » openjdk-7 » java » awt » [javadoc | source]
java.awt
public class: List [javadoc | source]
java.lang.Object
   java.awt.Component
      java.awt.List

All Implemented Interfaces:
    ItemSelectable, Accessible, MenuContainer, Serializable, ImageObserver

Direct Known Subclasses:
   

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.

Nested Class Summary:
protected class  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. 
Field Summary
 Vector items    A vector created to contain items which will become part of the List Component. 
 int rows    This field will represent the number of visible rows in the List Component. It is specified only once, and that is when the list component is actually created. It will never change. 
 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. multipleMode will be set to false if the list component is set to single selection, that is where the user can only select one item on the list at any one time. 
 int[] selected    selected is an array that will contain the indices of items that have been selected. 
 int visibleIndex    This variable contains the value that will be used when trying to make a particular list item visible. 
transient  ActionListener actionListener     
transient  ItemListener itemListener     
static final  int DEFAULT_VISIBLE_ROWS    The default number of visible rows is 4. A list with zero rows is unusable and unsightly. 
Fields inherited from java.awt.Component:
peer,  parent,  appContext,  x,  y,  width,  height,  foreground,  background,  font,  peerFont,  cursor,  locale,  bufferStrategy,  ignoreRepaint,  visible,  enabled,  dropTarget,  popups,  focusTraversalKeys,  LOCK,  minSize,  minSizeSet,  prefSize,  prefSizeSet,  maxSize,  maxSizeSet,  componentOrientation,  newEventsOnly,  componentListener,  focusListener,  hierarchyListener,  hierarchyBoundsListener,  keyListener,  mouseListener,  mouseMotionListener,  mouseWheelListener,  inputMethodListener,  windowClosingException,  actionListenerK,  adjustmentListenerK,  componentListenerK,  containerListenerK,  focusListenerK,  itemListenerK,  keyListenerK,  mouseListenerK,  mouseMotionListenerK,  mouseWheelListenerK,  textListenerK,  ownedWindowK,  windowListenerK,  inputMethodListenerK,  hierarchyListenerK,  hierarchyBoundsListenerK,  windowStateListenerK,  windowFocusListenerK,  eventMask,  isInc,  incRate,  TOP_ALIGNMENT,  CENTER_ALIGNMENT,  BOTTOM_ALIGNMENT,  LEFT_ALIGNMENT,  RIGHT_ALIGNMENT,  isPacked,  backgroundEraseDisabled,  eventCache,  accessibleContext
Constructor:
 public List() throws HeadlessException 
    Creates a new scrolling list. By default, there are four visible lines and multiple selections are not allowed. Note that this is a convenience method for List(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.
    Also see:
    java.awt.GraphicsEnvironment#isHeadless
    exception: HeadlessException - if GraphicsEnvironment.isHeadless() returns true.
 public List(int rows) throws HeadlessException 
    Creates 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 for List(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.
    Also see:
    java.awt.GraphicsEnvironment#isHeadless
    exception: HeadlessException - if GraphicsEnvironment.isHeadless() returns true.
    since: JDK1.1 -
 public List(int rows,
    boolean multipleMode) throws HeadlessException 
    Creates 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 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.
    Throws:
    HeadlessException - if GraphicsEnvironment.isHeadless() returns true.
    Also see:
    java.awt.GraphicsEnvironment#isHeadless
    exception: HeadlessException - if GraphicsEnvironment.isHeadless() returns true.
Method from java.awt.List Summary:
add,   add,   addActionListener,   addItem,   addItem,   addItemListener,   addNotify,   allowsMultipleSelections,   clear,   constructComponentName,   countItems,   delItem,   delItems,   deselect,   eventEnabled,   getAccessibleContext,   getActionListeners,   getItem,   getItemCount,   getItemImpl,   getItemListeners,   getItems,   getListeners,   getMinimumSize,   getMinimumSize,   getPreferredSize,   getPreferredSize,   getRows,   getSelectedIndex,   getSelectedIndexes,   getSelectedItem,   getSelectedItems,   getSelectedObjects,   getVisibleIndex,   isIndexSelected,   isMultipleMode,   isSelected,   makeVisible,   minimumSize,   minimumSize,   paramString,   preferredSize,   preferredSize,   processActionEvent,   processEvent,   processItemEvent,   remove,   remove,   removeActionListener,   removeAll,   removeItemListener,   removeNotify,   replaceItem,   select,   setMultipleMode,   setMultipleSelections
Methods from java.awt.Component:
action,   add,   addComponentListener,   addFocusListener,   addHierarchyBoundsListener,   addHierarchyListener,   addInputMethodListener,   addKeyListener,   addMouseListener,   addMouseMotionListener,   addMouseWheelListener,   addNotify,   addPropertyChangeListener,   addPropertyChangeListener,   adjustListeningChildrenOnParent,   applyComponentOrientation,   applyCompoundShape,   applyCurrentShape,   areBoundsValid,   areFocusTraversalKeysSet,   areInputMethodsEnabled,   autoProcessMouseWheel,   bounds,   canBeFocusOwner,   canBeFocusOwnerRecursively,   checkGD,   checkImage,   checkImage,   checkTreeLock,   checkWindowClosingException,   clearCurrentFocusCycleRootOnHide,   clearMostRecentFocusOwnerOnHide,   coalesceEvents,   constructComponentName,   contains,   contains,   containsFocus,   countHierarchyMembers,   createBufferStrategy,   createBufferStrategy,   createHierarchyEvents,   createImage,   createImage,   createVolatileImage,   createVolatileImage,   deliverEvent,   disable,   disableEvents,   dispatchEvent,   dispatchEventImpl,   dispatchMouseWheelToAncestor,   doLayout,   enable,   enable,   enableEvents,   enableInputMethods,   eventEnabled,   eventTypeEnabled,   findUnderMouseInWindow,   firePropertyChange,   firePropertyChange,   firePropertyChange,   firePropertyChange,   firePropertyChange,   firePropertyChange,   firePropertyChange,   firePropertyChange,   firePropertyChange,   getAccessControlContext,   getAccessibleContext,   getAccessibleIndexInParent,   getAccessibleStateSet,   getAlignmentX,   getAlignmentY,   getBackBuffer,   getBackground,   getBaseline,   getBaselineResizeBehavior,   getBounds,   getBounds,   getBoundsOp,   getBufferStrategy,   getColorModel,   getComponentAt,   getComponentAt,   getComponentListeners,   getComponentOrientation,   getContainer,   getContainingWindow,   getCursor,   getCursor_NoClientCode,   getDropTarget,   getFocusCycleRootAncestor,   getFocusListeners,   getFocusTraversalKeys,   getFocusTraversalKeysEnabled,   getFocusTraversalKeys_NoIDCheck,   getFont,   getFontMetrics,   getFont_NoClientCode,   getForeground,   getGraphics,   getGraphicsConfiguration,   getGraphicsConfiguration_NoClientCode,   getGraphics_NoClientCode,   getHWPeerAboveMe,   getHeight,   getHierarchyBoundsListeners,   getHierarchyListeners,   getIgnoreRepaint,   getInputContext,   getInputMethodListeners,   getInputMethodRequests,   getKeyListeners,   getListeners,   getLocale,   getLocation,   getLocation,   getLocationOnScreen,   getLocationOnScreen_NoTreeLock,   getLocationOnWindow,   getMaximumSize,   getMinimumSize,   getMouseListeners,   getMouseMotionListeners,   getMousePosition,   getMouseWheelListeners,   getName,   getNativeContainer,   getNextFocusCandidate,   getNormalShape,   getObjectLock,   getOpaqueShape,   getParent,   getParent_NoClientCode,   getPeer,   getPreferredSize,   getPropertyChangeListeners,   getPropertyChangeListeners,   getSiblingIndexAbove,   getSiblingIndexBelow,   getSize,   getSize,   getToolkit,   getToolkitImpl,   getTraversalRoot,   getTreeLock,   getWidth,   getX,   getY,   gotFocus,   handleEvent,   hasFocus,   hide,   imageUpdate,   initializeFocusTraversalKeys,   inside,   invalidate,   invalidateIfValid,   invalidateParent,   isAutoFocusTransferOnDisposal,   isBackgroundSet,   isCoalescingEnabled,   isCursorSet,   isDisplayable,   isDoubleBuffered,   isEnabled,   isEnabledImpl,   isFocusCycleRoot,   isFocusOwner,   isFocusTraversable,   isFocusTraversableOverridden,   isFocusable,   isFontSet,   isForegroundSet,   isInstanceOf,   isLightweight,   isMaximumSizeSet,   isMinimumSizeSet,   isMixingNeeded,   isNonOpaqueForMixing,   isOpaque,   isPreferredSizeSet,   isRecursivelyVisible,   isSameOrAncestorOf,   isShowing,   isValid,   isVisible,   isVisible_NoClientCode,   keyDown,   keyUp,   layout,   lightweightPaint,   lightweightPrint,   list,   list,   list,   list,   list,   locate,   location,   lostFocus,   minimumSize,   mixOnHiding,   mixOnReshaping,   mixOnShowing,   mixOnValidating,   mixOnZOrderChanging,   mouseDown,   mouseDrag,   mouseEnter,   mouseExit,   mouseMove,   mouseUp,   move,   nextFocus,   numListening,   paint,   paintAll,   paintHeavyweightComponents,   paramString,   pointRelativeToComponent,   postEvent,   postsOldMouseEvents,   preferredSize,   prepareImage,   prepareImage,   print,   printAll,   printHeavyweightComponents,   processComponentEvent,   processEvent,   processFocusEvent,   processHierarchyBoundsEvent,   processHierarchyEvent,   processInputMethodEvent,   processKeyEvent,   processMouseEvent,   processMouseMotionEvent,   processMouseWheelEvent,   relocateComponent,   remove,   removeComponentListener,   removeFocusListener,   removeHierarchyBoundsListener,   removeHierarchyListener,   removeInputMethodListener,   removeKeyListener,   removeMouseListener,   removeMouseMotionListener,   removeMouseWheelListener,   removeNotify,   removePropertyChangeListener,   removePropertyChangeListener,   repaint,   repaint,   repaint,   repaint,   requestFocus,   requestFocus,   requestFocus,   requestFocus,   requestFocusHelper,   requestFocusHelper,   requestFocusInWindow,   requestFocusInWindow,   requestFocusInWindow,   requestFocusInWindow,   reshape,   resize,   resize,   revalidate,   setAutoFocusTransferOnDisposal,   setBackground,   setBounds,   setBounds,   setBoundsOp,   setComponentOrientation,   setCursor,   setDropTarget,   setEnabled,   setFocusTraversalKeys,   setFocusTraversalKeysEnabled,   setFocusTraversalKeys_NoIDCheck,   setFocusable,   setFont,   setForeground,   setGraphicsConfiguration,   setIgnoreRepaint,   setLocale,   setLocation,   setLocation,   setMaximumSize,   setMinimumSize,   setName,   setPreferredSize,   setRequestFocusController,   setSize,   setSize,   setVisible,   show,   show,   size,   subtractAndApplyShape,   subtractAndApplyShapeBelowMe,   toString,   transferFocus,   transferFocus,   transferFocusBackward,   transferFocusBackward,   transferFocusUpCycle,   update,   updateCursorImmediately,   updateGraphicsData,   updateZOrder,   validate
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from java.awt.List Detail:
 public  void add(String item) 
    Adds the specified item to the end of scrolling list.
 public  void 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.
 public synchronized  void 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 or types Enter when the list has the keyboard focus.

    If listener l is null, no exception is thrown and no action is performed.

    Refer to AWT Threading Issues for details on AWT's threading model.

 public  void addItem(String item) 
Deprecated! replaced - by add(String).

 public synchronized  void addItem(String item,
    int index) 
Deprecated! replaced - by add(String, int).

 public synchronized  void addItemListener(ItemListener l) 
    Adds 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 to select or deselect. If listener l is null, no exception is thrown and no action is performed.

    Refer to AWT Threading Issues for details on AWT's threading model.

 public  void addNotify() 
    Creates the peer for the list. The peer allows us to modify the list's appearance without changing its functionality.
 public boolean allowsMultipleSelections() 
Deprecated! As - of JDK version 1.1, replaced by isMultipleMode().

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

 String constructComponentName() 
    Construct a name for this component. Called by getName when the name is null.
 public int countItems() 
Deprecated! As - of JDK version 1.1, replaced by getItemCount().

 public  void delItem(int position) 
Deprecated! replaced - by remove(String) and remove(int).

 public synchronized  void 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.

 public synchronized  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.

 boolean eventEnabled(AWTEvent e) 
 public AccessibleContext 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.
 public synchronized ActionListener[] getActionListeners() 
    Returns an array of all the action listeners registered on this list.
 public String getItem(int index) 
    Gets the item associated with the specified index.
 public int getItemCount() 
    Gets the number of items in the list.
 final String getItemImpl(int index) 
 public synchronized ItemListener[] getItemListeners() 
    Returns an array of all the item listeners registered on this list.
 public synchronized String[] getItems() 
    Gets the items in the list.
 public T[] getListeners(Class<T> listenerType) 
    Returns an array of all the objects currently registered as FooListeners upon this List. FooListeners are registered using the addFooListener method.

    You can specify the listenerType argument with a class literal, such as FooListener.class. For example, you can query a List l for 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.
 public Dimension getMinimumSize() 
    Determines the minimum size of this scrolling list.
 public Dimension getMinimumSize(int rows) 
    Gets the minumum dimensions for a list with the specified number of rows.
 public Dimension getPreferredSize() 
    Gets the preferred size of this scrolling list.
 public Dimension getPreferredSize(int rows) 
    Gets the preferred dimensions for a list with the specified number of rows.
 public int getRows() 
    Gets the number of visible lines in this list. Note that once the List has been created, this number will never change.
 public synchronized int getSelectedIndex() 
    Gets the index of the selected item on the list,
 public synchronized int[] getSelectedIndexes() 
    Gets the selected indexes on the list.
 public synchronized String getSelectedItem() 
    Gets the selected item on this scrolling list.
 public synchronized String[] getSelectedItems() 
    Gets the selected items on this scrolling list.
 public Object[] getSelectedObjects() 
    Gets the selected items on this scrolling list in an array of Objects.
 public int getVisibleIndex() 
    Gets the index of the item that was last made visible by the method makeVisible.
 public boolean isIndexSelected(int index) 
    Determines if the specified item in this scrolling list is selected.
 public boolean isMultipleMode() 
    Determines whether this list allows multiple selections.
 public boolean isSelected(int index) 
Deprecated! As - of JDK version 1.1, replaced by isIndexSelected(int).

 public synchronized  void makeVisible(int index) 
    Makes the item at the specified index visible.
 public Dimension minimumSize() 
Deprecated! As - of JDK version 1.1, replaced by getMinimumSize().

 public Dimension minimumSize(int rows) 
Deprecated! As - of JDK version 1.1, replaced by getMinimumSize(int).

 protected String paramString() 
    Returns the parameter string representing the state of this scrolling list. This string is useful for debugging.
 public Dimension preferredSize() 
Deprecated! As - of JDK version 1.1, replaced by getPreferredSize().

 public Dimension preferredSize(int rows) 
Deprecated! As - of JDK version 1.1, replaced by getPreferredSize(int).

 protected  void 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.

    Note that if the event parameter is null the behavior is unspecified and may result in an exception.

 protected  void 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.

    Note that if the event parameter is null the behavior is unspecified and may result in an exception.

 protected  void 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.

    Note that if the event parameter is null the behavior is unspecified and may result in an exception.

 public synchronized  void remove(String item) 
    Removes 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.
 public  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.
 public synchronized  void 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 listener l is null, no exception is thrown and no action is performed.

    Refer to AWT Threading Issues for details on AWT's threading model.

 public  void removeAll() 
    Removes all items from this list.
 public synchronized  void removeItemListener(ItemListener l) 
    Removes the specified item listener so that it no longer receives item events from this list. If listener l is null, no exception is thrown and no action is performed.

    Refer to AWT Threading Issues for details on AWT's threading model.

 public  void removeNotify() 
    Removes the peer for this list. The peer allows us to modify the list's appearance without changing its functionality.
 public synchronized  void replaceItem(String newValue,
    int index) 
    Replaces the item at the specified index in the scrolling list with the new string.
 public  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 an ItemEvent is by user interaction.

 public  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.
 public synchronized  void setMultipleSelections(boolean b) 
Deprecated! As - of JDK version 1.1, replaced by setMultipleMode(boolean).