Class List

  • All Implemented Interfaces:
    Drawable

    public class List
    extends Scrollable
    Instances of this class represent a selectable user interface object that displays a list of strings and issues notification when a string is selected. A list may be single or multi select.
    Styles:
    SINGLE, MULTI
    Events:
    Selection, DefaultSelection

    Note: Only one of SINGLE and MULTI may be specified.

    IMPORTANT: This class is not intended to be subclassed.

    See Also:
    List snippets, SWT Example: ControlExample, Sample code and further information
    Restriction:
    This class is not intended to be subclassed by clients.
    • Constructor Detail

      • List

        public List​(Composite parent,
                    int style)
        Constructs a new instance of this class given its parent and a style value describing its behavior and appearance.

        The style value is either one of the style constants defined in class SWT which is applicable to instances of this class, or must be built by bitwise OR'ing together (that is, using the int "|" operator) two or more of those SWT style constants. The class description lists the style constants that are applicable to the class. Style bits are also inherited from superclasses.

        Parameters:
        parent - a composite control which will be the parent of the new instance (cannot be null)
        style - the style of control to construct
        Throws:
        IllegalArgumentException -
        • ERROR_NULL_ARGUMENT - if the parent is null
        SWTException -
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent
        • ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass
        See Also:
        SWT.SINGLE, SWT.MULTI, Widget.checkSubclass(), Widget.getStyle()
    • Method Detail

      • add

        public void add​(String string)
        Adds the argument to the end of the receiver's list.

        Note: If control characters like '\n', '\t' etc. are used in the string, then the behavior is platform dependent.

        Parameters:
        string - the new item
        Throws:
        IllegalArgumentException -
        • ERROR_NULL_ARGUMENT - if the string is null
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
        See Also:
        add(String,int)
      • add

        public void add​(String string,
                        int index)
        Adds the argument to the receiver's list at the given zero-relative index.

        Note: To add an item at the end of the list, use the result of calling getItemCount() as the index or use add(String).

        Also note, if control characters like '\n', '\t' etc. are used in the string, then the behavior is platform dependent.

        Parameters:
        string - the new item
        index - the index for the item
        Throws:
        IllegalArgumentException -
        • ERROR_NULL_ARGUMENT - if the string is null
        • ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list (inclusive)
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
        See Also:
        add(String)
      • addSelectionListener

        public void addSelectionListener​(SelectionListener listener)
        Adds the listener to the collection of listeners who will be notified when the user changes the receiver's selection, by sending it one of the messages defined in the SelectionListener interface.

        widgetSelected is called when the selection changes. widgetDefaultSelected is typically called when an item is double-clicked.

        Parameters:
        listener - the listener which should be notified when the user changes the receiver's selection
        Throws:
        IllegalArgumentException -
        • ERROR_NULL_ARGUMENT - if the listener is null
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
        See Also:
        SelectionListener, removeSelectionListener(org.eclipse.swt.events.SelectionListener), SelectionEvent
      • deselect

        public void deselect​(int[] indices)
        Deselects the items at the given zero-relative indices in the receiver. If the item at the given zero-relative index in the receiver is selected, it is deselected. If the item at the index was not selected, it remains deselected. Indices that are out of range and duplicate indices are ignored.
        Parameters:
        indices - the array of indices for the items to deselect
        Throws:
        IllegalArgumentException -
        • ERROR_NULL_ARGUMENT - if the set of indices is null
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
      • deselect

        public void deselect​(int index)
        Deselects the item at the given zero-relative index in the receiver. If the item at the index was already deselected, it remains deselected. Indices that are out of range are ignored.
        Parameters:
        index - the index of the item to deselect
        Throws:
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
      • deselect

        public void deselect​(int start,
                             int end)
        Deselects the items at the given zero-relative indices in the receiver. If the item at the given zero-relative index in the receiver is selected, it is deselected. If the item at the index was not selected, it remains deselected. The range of the indices is inclusive. Indices that are out of range are ignored.
        Parameters:
        start - the start index of the items to deselect
        end - the end index of the items to deselect
        Throws:
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
      • deselectAll

        public void deselectAll()
        Deselects all selected items in the receiver.
        Throws:
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
      • getFocusIndex

        public int getFocusIndex()
        Returns the zero-relative index of the item which currently has the focus in the receiver, or -1 if no item has focus.
        Returns:
        the index of the selected item
        Throws:
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
      • getItem

        public String getItem​(int index)
        Returns the item at the given, zero-relative index in the receiver. Throws an exception if the index is out of range.
        Parameters:
        index - the index of the item to return
        Returns:
        the item at the given index
        Throws:
        IllegalArgumentException -
        • ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
      • getItemCount

        public int getItemCount()
        Returns the number of items contained in the receiver.
        Returns:
        the number of items
        Throws:
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
      • getItemHeight

        public int getItemHeight()
        Returns the height of the area which would be used to display one of the items in the list.
        Returns:
        the height of one item
        Throws:
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
      • getItems

        public String[] getItems()
        Returns a (possibly empty) array of Strings which are the items in the receiver.

        Note: This is not the actual structure used by the receiver to maintain its list of items, so modifying the array will not affect the receiver.

        Returns:
        the items in the receiver's list
        Throws:
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
      • getSelection

        public String[] getSelection()
        Returns an array of Strings that are currently selected in the receiver. The order of the items is unspecified. An empty array indicates that no items are selected.

        Note: This is not the actual structure used by the receiver to maintain its selection, so modifying the array will not affect the receiver.

        Returns:
        an array representing the selection
        Throws:
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
      • getSelectionCount

        public int getSelectionCount()
        Returns the number of selected items contained in the receiver.
        Returns:
        the number of selected items
        Throws:
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
      • getSelectionIndex

        public int getSelectionIndex()
        Returns the zero-relative index of the item which is currently selected in the receiver, or -1 if no item is selected.
        Returns:
        the index of the selected item or -1
        Throws:
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
      • getSelectionIndices

        public int[] getSelectionIndices()
        Returns the zero-relative indices of the items which are currently selected in the receiver. The order of the indices is unspecified. The array is empty if no items are selected.

        Note: This is not the actual structure used by the receiver to maintain its selection, so modifying the array will not affect the receiver.

        Returns:
        the array of indices of the selected items
        Throws:
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
      • getTopIndex

        public int getTopIndex()
        Returns the zero-relative index of the item which is currently at the top of the receiver. This index can change when items are scrolled or new items are added or removed.
        Returns:
        the index of the top item
        Throws:
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
      • indexOf

        public int indexOf​(String string)
        Gets the index of an item.

        The list is searched starting at 0 until an item is found that is equal to the search item. If no item is found, -1 is returned. Indexing is zero based.

        Parameters:
        string - the search item
        Returns:
        the index of the item
        Throws:
        IllegalArgumentException -
        • ERROR_NULL_ARGUMENT - if the string is null
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
      • indexOf

        public int indexOf​(String string,
                           int start)
        Searches the receiver's list starting at the given, zero-relative index until an item is found that is equal to the argument, and returns the index of that item. If no item is found or the starting index is out of range, returns -1.
        Parameters:
        string - the search item
        start - the zero-relative index at which to start the search
        Returns:
        the index of the item
        Throws:
        IllegalArgumentException -
        • ERROR_NULL_ARGUMENT - if the string is null
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
      • isSelected

        public boolean isSelected​(int index)
        Returns true if the item is selected, and false otherwise. Indices out of range are ignored.
        Parameters:
        index - the index of the item
        Returns:
        the selection state of the item at the index
        Throws:
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
      • remove

        public void remove​(int[] indices)
        Removes the items from the receiver at the given zero-relative indices.
        Parameters:
        indices - the array of indices of the items
        Throws:
        IllegalArgumentException -
        • ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)
        • ERROR_NULL_ARGUMENT - if the indices array is null
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
      • remove

        public void remove​(int index)
        Removes the item from the receiver at the given zero-relative index.
        Parameters:
        index - the index for the item
        Throws:
        IllegalArgumentException -
        • ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
      • remove

        public void remove​(int start,
                           int end)
        Removes the items from the receiver which are between the given zero-relative start and end indices (inclusive).
        Parameters:
        start - the start of the range
        end - the end of the range
        Throws:
        IllegalArgumentException -
        • ERROR_INVALID_RANGE - if either the start or end are not between 0 and the number of elements in the list minus 1 (inclusive)
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
      • remove

        public void remove​(String string)
        Searches the receiver's list starting at the first item until an item is found that is equal to the argument, and removes that item from the list.
        Parameters:
        string - the item to remove
        Throws:
        IllegalArgumentException -
        • ERROR_NULL_ARGUMENT - if the string is null
        • ERROR_INVALID_ARGUMENT - if the string is not found in the list
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
      • removeAll

        public void removeAll()
        Removes all of the items from the receiver.
        Throws:
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
      • select

        public void select​(int[] indices)
        Selects the items at the given zero-relative indices in the receiver. The current selection is not cleared before the new items are selected.

        If the item at a given index is not selected, it is selected. If the item at a given index was already selected, it remains selected. Indices that are out of range and duplicate indices are ignored. If the receiver is single-select and multiple indices are specified, then all indices are ignored.

        Parameters:
        indices - the array of indices for the items to select
        Throws:
        IllegalArgumentException -
        • ERROR_NULL_ARGUMENT - if the array of indices is null
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
        See Also:
        setSelection(int[])
      • select

        public void select​(int index)
        Selects the item at the given zero-relative index in the receiver's list. If the item at the index was already selected, it remains selected. Indices that are out of range are ignored.
        Parameters:
        index - the index of the item to select
        Throws:
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
      • select

        public void select​(int start,
                           int end)
        Selects the items in the range specified by the given zero-relative indices in the receiver. The range of indices is inclusive. The current selection is not cleared before the new items are selected.

        If an item in the given range is not selected, it is selected. If an item in the given range was already selected, it remains selected. Indices that are out of range are ignored and no items will be selected if start is greater than end. If the receiver is single-select and there is more than one item in the given range, then all indices are ignored.

        Parameters:
        start - the start of the range
        end - the end of the range
        Throws:
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
        See Also:
        setSelection(int,int)
      • selectAll

        public void selectAll()
        Selects all of the items in the receiver.

        If the receiver is single-select, do nothing.

        Throws:
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
      • setFont

        public void setFont​(Font font)
        Description copied from class: Control
        Sets the font that the receiver will use to paint textual information to the font specified by the argument, or to the default font for that kind of control if the argument is null.
        Overrides:
        setFont in class Control
        Parameters:
        font - the new font (or null)
      • setItem

        public void setItem​(int index,
                            String string)
        Sets the text of the item in the receiver's list at the given zero-relative index to the string argument.
        Parameters:
        index - the index for the item
        string - the new text for the item
        Throws:
        IllegalArgumentException -
        • ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)
        • ERROR_NULL_ARGUMENT - if the string is null
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
      • setItems

        public void setItems​(String... items)
        Sets the receiver's items to be the given array of items.
        Parameters:
        items - the array of items
        Throws:
        IllegalArgumentException -
        • ERROR_NULL_ARGUMENT - if the items array is null
        • ERROR_INVALID_ARGUMENT - if an item in the items array is null
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
      • setSelection

        public void setSelection​(int[] indices)
        Selects the items at the given zero-relative indices in the receiver. The current selection is cleared before the new items are selected, and if necessary the receiver is scrolled to make the new selection visible.

        Indices that are out of range and duplicate indices are ignored. If the receiver is single-select and multiple indices are specified, then all indices are ignored.

        Parameters:
        indices - the indices of the items to select
        Throws:
        IllegalArgumentException -
        • ERROR_NULL_ARGUMENT - if the array of indices is null
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
        See Also:
        deselectAll(), select(int[])
      • setSelection

        public void setSelection​(String[] items)
        Sets the receiver's selection to be the given array of items. The current selection is cleared before the new items are selected, and if necessary the receiver is scrolled to make the new selection visible.

        Items that are not in the receiver are ignored. If the receiver is single-select and multiple items are specified, then all items are ignored.

        Parameters:
        items - the array of items
        Throws:
        IllegalArgumentException -
        • ERROR_NULL_ARGUMENT - if the array of items is null
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
        See Also:
        deselectAll(), select(int[]), setSelection(int[])
      • setSelection

        public void setSelection​(int index)
        Selects the item at the given zero-relative index in the receiver. If the item at the index was already selected, it remains selected. The current selection is first cleared, then the new item is selected, and if necessary the receiver is scrolled to make the new selection visible. Indices that are out of range are ignored.
        Parameters:
        index - the index of the item to select
        Throws:
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
        See Also:
        deselectAll(), select(int)
      • setSelection

        public void setSelection​(int start,
                                 int end)
        Selects the items in the range specified by the given zero-relative indices in the receiver. The range of indices is inclusive. The current selection is cleared before the new items are selected, and if necessary the receiver is scrolled to make the new selection visible.

        Indices that are out of range are ignored and no items will be selected if start is greater than end. If the receiver is single-select and there is more than one item in the given range, then all indices are ignored.

        Parameters:
        start - the start index of the items to select
        end - the end index of the items to select
        Throws:
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
        See Also:
        deselectAll(), select(int,int)
      • setTopIndex

        public void setTopIndex​(int index)
        Sets the zero-relative index of the item which is currently at the top of the receiver. This index can change when items are scrolled or new items are added and removed.
        Parameters:
        index - the index of the top item
        Throws:
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
      • showSelection

        public void showSelection()
        Shows the selection. If the selection is already showing in the receiver, this method simply returns. Otherwise, the items are scrolled until the selection is visible.
        Throws:
        SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver