Class FilteredListModel<E>

  • All Implemented Interfaces:
    java.io.Serializable, javax.swing.ListModel<E>

    public class FilteredListModel<E>
    extends javax.swing.AbstractListModel<E>
    A model for JLists that supports filtering. The ListModel methods getElementAt and getSize return values appropriate for the applied filter. It is recommended that you get and set the selection using values rather than indices.
    Since:
    3.0
    Author:
    Chris Jennings
    See Also:
    Serialized Form
    • Constructor Detail

      • FilteredListModel

        public FilteredListModel()
      • FilteredListModel

        public FilteredListModel​(E[] items)
      • FilteredListModel

        public FilteredListModel​(java.util.Collection<? extends E> items)
    • Method Detail

      • add

        public void add​(E item)
      • add

        public void add​(int index,
                        E item)
      • getItem

        public E getItem​(int index)
        Returns the item at the unfiltered index.
        Parameters:
        index - the model index
        Returns:
        the item at the specified index
      • getItemCount

        public int getItemCount()
        Returns the size of the unfiltered list.
        Returns:
        the unfiltered model size
      • clear

        public void clear()
        Removes all elements from the list.
      • getUnfilteredIndex

        public int getUnfilteredIndex​(java.lang.Object o)
        Returns the index of the first object equal to o, ignoring the current filter. This is useful when the list is an index into a list of objects and the indexed object must be retrieved.
        Parameters:
        o - the object to find the index of
        Returns:
        the unfiltered index of the first such object, or -1
      • linkTo

        public void linkTo​(JFilterField field,
                           javax.swing.JList<E> list,
                           boolean restoreSelection)
        This is a convenience method that attaches this model to the current document of the specified search field. When the text in the document changes, the text from the field will be used to create a string filter, which will then be applied to the list.
        Parameters:
        field - the field to link this model to
        list - the list whose items are filtered
        restoreSelection - if true, any selection is restore after updating the list
        T - the item type of the target list
      • stashSelection

        public static <E> java.util.List<E> stashSelection​(javax.swing.JList<E> list)
        Captures the current selection for use with restoreSelection(JList, List).
      • restoreSelection

        public static <E> void restoreSelection​(javax.swing.JList<E> list,
                                                java.util.List<E> selection)
        Resores a previously stashed selection. This can be used to restore the selection after the filter has been updated.
        Parameters:
        list - the list to restore the selection to
        selection - the selection to restore
      • getElementAt

        public E getElementAt​(int index)
      • getSize

        public int getSize()
      • acceptAllFilter

        public static <T> FilteredListModel.ListFilter<T> acceptAllFilter()
        Returns a filter that accepts all items, of a type matching that of the list model.
        Type Parameters:
        T - the type of the list model
        Returns:
        a filter that accepts all items