Module java.desktop
Package java.awt

Class List

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


    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:

    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.

    Since:
    1.0
    See Also:
    ItemEvent, ItemListener, ActionEvent, ActionListener, Serialized Form
    • Constructor Detail

      • List

        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.
        See Also:
        GraphicsEnvironment.isHeadless()
      • List

        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.
        Since:
        1.1
        See Also:
        GraphicsEnvironment.isHeadless()
      • List

        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.
        See Also:
        GraphicsEnvironment.isHeadless()
    • Method Detail

      • getItemCount

        public int getItemCount​()
        Gets the number of items in the list.
        Returns:
        the number of items in the list
        Since:
        1.1
        See Also:
        getItem(int)
      • countItems

        @Deprecated
        public int countItems​()
        Deprecated. As of JDK version 1.1, replaced by getItemCount().
        Returns the number of items in the list.
        Returns:
        the number of items in the list
      • getItem

        public String 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()
      • add

        public void add​(String item)
        Adds the specified item to the end of scrolling list.
        Parameters:
        item - the item to be added
        Since:
        1.1
      • addItem

        @Deprecated
        public void addItem​(String item)
        Deprecated. replaced by add(String).
        Adds the specified item to the end of the list.
        Parameters:
        item - the item to be added
      • add

        public void add​(String item,
                        int index)
        Adds 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 null then the item is treated as an empty string, ""
        index - the position at which to add the item
        Since:
        1.1
      • addItem

        @Deprecated
        public void addItem​(String item,
                            int index)
        Deprecated. replaced by add(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
      • replaceItem

        public void 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
        Throws:
        ArrayIndexOutOfBoundsException - if index is out of range
      • clear

        @Deprecated
        public void clear​()
        Deprecated. As of JDK version 1.1, replaced by removeAll().
      • remove

        public 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.
        Parameters:
        item - the item to remove from the list
        Throws:
        IllegalArgumentException - if the item doesn't exist in the list
        Since:
        1.1
      • remove

        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.
        Parameters:
        position - the index of the item to delete
        Throws:
        ArrayIndexOutOfBoundsException - if the position is less than 0 or greater than getItemCount()-1
        Since:
        1.1
        See Also:
        add(String, int)
      • delItem

        @Deprecated
        public void delItem​(int position)
        Deprecated. replaced by remove(String) and remove(int).
        Removes the item at the specified position.
        Parameters:
        position - the index of the item to delete
      • getSelectedIndex

        public 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, -1 is returned.
        See Also:
        select(int), deselect(int), isIndexSelected(int)
      • getSelectedIndexes

        public 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:
        select(int), deselect(int), isIndexSelected(int)
      • getSelectedItem

        public String getSelectedItem​()
        Gets 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, null is returned.
        See Also:
        select(int), deselect(int), isIndexSelected(int)
      • getSelectedItems

        public String[] getSelectedItems​()
        Gets 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:
        select(int), deselect(int), isIndexSelected(int)
      • getSelectedObjects

        public Object[] getSelectedObjects​()
        Gets the selected items on this scrolling list in an array of Objects.
        Specified by:
        getSelectedObjects in 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:
        getSelectedItems(), ItemSelectable
      • select

        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.

        Parameters:
        index - the position of the item to select
        See Also:
        getSelectedItem(), deselect(int), isIndexSelected(int)
      • deselect

        public 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:
        select(int), getSelectedItem(), isIndexSelected(int)
      • isIndexSelected

        public boolean 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:
        1.1
        See Also:
        select(int), deselect(int)
      • isSelected

        @Deprecated
        public boolean isSelected​(int index)
        Deprecated. As of JDK version 1.1, replaced by isIndexSelected(int).
        Determines if the specified item in the list is selected.
        Parameters:
        index - specifies the item to be checked
        Returns:
        true if the item is selected; otherwise false
      • getRows

        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.
        Returns:
        the number of visible lines in this scrolling list
      • isMultipleMode

        public boolean isMultipleMode​()
        Determines whether this list allows multiple selections.
        Returns:
        true if this list allows multiple selections; otherwise, false
        Since:
        1.1
        See Also:
        setMultipleMode(boolean)
      • allowsMultipleSelections

        @Deprecated
        public boolean allowsMultipleSelections​()
        Deprecated. As of JDK version 1.1, replaced by isMultipleMode().
        Determines whether this list allows multiple selections.
        Returns:
        true if this list allows multiple selections; otherwise false
      • setMultipleMode

        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.
        Parameters:
        b - if true then multiple selections are allowed; otherwise, only one item from the list can be selected at once
        Since:
        1.1
        See Also:
        isMultipleMode()
      • setMultipleSelections

        @Deprecated
        public void setMultipleSelections​(boolean b)
        Deprecated. As of JDK version 1.1, replaced by setMultipleMode(boolean).
        Enables or disables multiple selection mode for this list.
        Parameters:
        b - true to enable multiple mode, false otherwise
      • getVisibleIndex

        public int 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)
      • makeVisible

        public void makeVisible​(int index)
        Makes the item at the specified index visible.
        Parameters:
        index - the position of the item
        See Also:
        getVisibleIndex()
      • getPreferredSize

        public Dimension 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:
        1.1
        See Also:
        Component.getPreferredSize()
      • preferredSize

        @Deprecated
        public Dimension preferredSize​(int 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.
        Parameters:
        rows - the number of rows
        Returns:
        the preferred dimensions for displaying this list
      • preferredSize

        @Deprecated
        public Dimension preferredSize​()
        Deprecated. As of JDK version 1.1, replaced by getPreferredSize().
        Description copied from class: Component
        Returns the component's preferred size.
        Overrides:
        preferredSize in class Component
        Returns:
        the component's preferred size
      • getMinimumSize

        public Dimension getMinimumSize​(int rows)
        Gets 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:
        Component.getMinimumSize()
      • minimumSize

        @Deprecated
        public Dimension minimumSize​(int 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.
        Parameters:
        rows - the number of rows in the list
        Returns:
        the minimum dimensions for displaying this list
      • minimumSize

        @Deprecated
        public Dimension minimumSize​()
        Deprecated. As of JDK version 1.1, replaced by getMinimumSize().
        Description copied from class: Component
        Returns the minimum size of this component.
        Overrides:
        minimumSize in class Component
        Returns:
        the minimum size of this component
      • getListeners

        public <T extends EventListener> 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.
        Overrides:
        getListeners in 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 listenerType doesn't specify a class or interface that implements java.util.EventListener
        Since:
        1.3
        See Also:
        getItemListeners()
      • processItemEvent

        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.

        Parameters:
        e - the item event
        Since:
        1.1
        See Also:
        ItemEvent, ItemListener, addItemListener(java.awt.event.ItemListener), Component.enableEvents(long)
      • processActionEvent

        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.

        Parameters:
        e - the action event
        Since:
        1.1
        See Also:
        ActionEvent, ActionListener, addActionListener(java.awt.event.ActionListener), Component.enableEvents(long)
      • paramString

        protected String 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
      • delItems

        @Deprecated
        public 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.
        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
      • getAccessibleContext

        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.
        Specified by:
        getAccessibleContext in interface Accessible
        Overrides:
        getAccessibleContext in class Component
        Returns:
        an AccessibleAWTList that serves as the AccessibleContext of this List
        Since:
        1.3