Class DefaultFilteredTableModel<R,​C>

  • Type Parameters:
    R - the type representing the rows in this table model
    C - the type used to identify columns in this table model, Integer for indexed identification for example
    All Implemented Interfaces:
    FilteredModel<R>, FilteredTableModel<R,​C>, Serializable, TableModel
    Direct Known Subclasses:
    SwingEntityTableModel

    public class DefaultFilteredTableModel<R,​C>
    extends AbstractTableModel
    implements FilteredTableModel<R,​C>
    A TableModel implementation that supports filtering, searching and sorting.
     DefaultFilteredTableModel tableModel = ...;
     JTable table = new JTable(tableModel, tableModel.columnModel(), tableModel.selectionModel());
     

    User: Björn Darri
    Date: 18.4.2010
    Time: 09:48:07
    See Also:
    Serialized Form
    • Constructor Detail

      • DefaultFilteredTableModel

        public DefaultFilteredTableModel​(List<TableColumn> tableColumns,
                                         FilteredTableModel.ColumnValueProvider<R,​C> columnValueProvider)
        Instantiates a new table model.
        Parameters:
        tableColumns - the table columns to base this table model on
        columnValueProvider - the column value provider
      • DefaultFilteredTableModel

        public DefaultFilteredTableModel​(List<TableColumn> tableColumns,
                                         FilteredTableModel.ColumnValueProvider<R,​C> columnValueProvider,
                                         Collection<? extends ColumnFilterModel<R,​C,​?>> columnFilterModels)
        Instantiates a new table model.
        Parameters:
        tableColumns - the table columns to base this table model on
        columnValueProvider - the column value provider
        columnFilterModels - the filter models if any, may be null
    • Method Detail

      • visibleItems

        public final List<R> visibleItems()
        Specified by:
        visibleItems in interface FilteredModel<R>
        Returns:
        an unmodifiable view of the visible items
      • filteredItems

        public final List<R> filteredItems()
        Specified by:
        filteredItems in interface FilteredModel<R>
        Returns:
        an unmodifiable view of the filtered items
      • visibleItemCount

        public final int visibleItemCount()
        Specified by:
        visibleItemCount in interface FilteredModel<R>
        Returns:
        the number of currently visible items
      • filteredItemCount

        public final int filteredItemCount()
        Specified by:
        filteredItemCount in interface FilteredModel<R>
        Returns:
        the number of currently filtered items
      • containsItem

        public final boolean containsItem​(R item)
        Description copied from interface: FilteredModel
        Returns true if this model contains the given item, visible or filtered.
        Specified by:
        containsItem in interface FilteredModel<R>
        Parameters:
        item - the item
        Returns:
        true if this model contains the item
      • isVisible

        public final boolean isVisible​(R item)
        Description copied from interface: FilteredModel
        Returns true if this model contains the given item, and it is visible, that is, not filtered
        Specified by:
        isVisible in interface FilteredModel<R>
        Parameters:
        item - the item
        Returns:
        true if the given item is visible
      • isFiltered

        public final boolean isFiltered​(R item)
        Description copied from interface: FilteredModel
        Returns true if this model contains the given item, and it is filtered, that is, is not visible
        Specified by:
        isFiltered in interface FilteredModel<R>
        Parameters:
        item - the item
        Returns:
        true if the given item is filtered
      • columnFilterModel

        public final <T> ColumnFilterModel<R,​C,​T> columnFilterModel​(C columnIdentifier)
        Description copied from interface: FilteredTableModel
        Returns the ColumnConditionModel for the column with the given identifier.
        Specified by:
        columnFilterModel in interface FilteredTableModel<R,​C>
        Type Parameters:
        T - the column value type
        Parameters:
        columnIdentifier - the column identifier
        Returns:
        the ColumnConditionModel for the column with the given identifier.
      • values

        public final <T> Collection<T> values​(C columnIdentifier)
        Specified by:
        values in interface FilteredTableModel<R,​C>
        Type Parameters:
        T - the value type
        Parameters:
        columnIdentifier - the identifier of the column for which to retrieve the values
        Returns:
        the values (including nulls) of the column identified by the given identifier from the visible rows in the table model
      • selectedValues

        public final <T> Collection<T> selectedValues​(C columnIdentifier)
        Specified by:
        selectedValues in interface FilteredTableModel<R,​C>
        Type Parameters:
        T - the value type
        Parameters:
        columnIdentifier - the identifier of the column for which to retrieve the values
        Returns:
        the values (including nulls) of the column identified by the given identifier from the selected rows in the table model
      • setMergeOnRefresh

        public final void setMergeOnRefresh​(boolean mergeOnRefresh)
        Specified by:
        setMergeOnRefresh in interface FilteredTableModel<R,​C>
        Parameters:
        mergeOnRefresh - true if merge on refresh should be enabled
      • isAsyncRefresh

        public final boolean isAsyncRefresh()
        Specified by:
        isAsyncRefresh in interface FilteredTableModel<R,​C>
        Returns:
        true if asynchronous refreshing is enabled, true by default
      • setAsyncRefresh

        public final void setAsyncRefresh​(boolean asyncRefresh)
        Description copied from interface: FilteredTableModel
        Sometimes we'd like to be able to refresh one or more table models and perform some action on the refreshed data, after the refresh has finished, such as selecting a particular entity or such. This is quite difficult to achieve with asynchronous refresh enabled, so here's a way to temporarily disable asynchronous refresh, for a more consistent/predictable behaviour.
        Specified by:
        setAsyncRefresh in interface FilteredTableModel<R,​C>
        Parameters:
        asyncRefresh - true if asynchronous refreshing should be enabled, true by default
      • itemAt

        public final R itemAt​(int rowIndex)
        Specified by:
        itemAt in interface FilteredTableModel<R,​C>
        Parameters:
        rowIndex - the row index
        Returns:
        the item at the given row index in the table model
      • indexOf

        public final int indexOf​(R item)
        Specified by:
        indexOf in interface FilteredTableModel<R,​C>
        Parameters:
        item - the item
        Returns:
        the index of the item in the table model
      • getIncludeCondition

        public final Predicate<R> getIncludeCondition()
        Description copied from interface: FilteredModel
        Returns the include condition used by this model, null if no include condition has been set.
        Specified by:
        getIncludeCondition in interface FilteredModel<R>
        Returns:
        the include condition
      • setIncludeCondition

        public final void setIncludeCondition​(Predicate<R> includeCondition)
        Description copied from interface: FilteredModel
        Sets the include condition and filters the model
        Specified by:
        setIncludeCondition in interface FilteredModel<R>
        Parameters:
        includeCondition - the Predicate to use when filtering, null if no filtering should be performed
      • removeItem

        public final void removeItem​(R item)
        Description copied from interface: FilteredTableModel
        Removes the given item from this table model
        Specified by:
        removeItem in interface FilteredTableModel<R,​C>
        Parameters:
        item - the item to remove from the model
      • removeItemAt

        public final void removeItemAt​(int index)
        Description copied from interface: FilteredTableModel
        Removes from this table model the visible element whose index is between index
        Specified by:
        removeItemAt in interface FilteredTableModel<R,​C>
        Parameters:
        index - the index of the row to be removed
      • removeItems

        public final void removeItems​(int fromIndex,
                                      int toIndex)
        Description copied from interface: FilteredTableModel
        Removes from this table model all visible elements whose index is between fromIndex, inclusive and toIndex, exclusive
        Specified by:
        removeItems in interface FilteredTableModel<R,​C>
        Parameters:
        fromIndex - index of first row to be removed
        toIndex - index after last row to be removed
      • getColumnClass

        public final Class<?> getColumnClass​(C columnIdentifier)
        Description copied from interface: FilteredTableModel
        Returns the class of the column with the given identifier
        Specified by:
        getColumnClass in interface FilteredTableModel<R,​C>
        Parameters:
        columnIdentifier - the column identifier
        Returns:
        the Class representing the given column
      • getValueAt

        public final Object getValueAt​(int rowIndex,
                                       int columnIndex)
        Specified by:
        getValueAt in interface TableModel
      • getStringAt

        public final String getStringAt​(int rowIndex,
                                        C columnIdentifier)
        Description copied from interface: FilteredTableModel
        Returns a String representation of the value for the given row and column.
        Specified by:
        getStringAt in interface FilteredTableModel<R,​C>
        Parameters:
        rowIndex - the row index
        columnIdentifier - the column identifier
        Returns:
        the string value
      • addFilterListener

        public final void addFilterListener​(EventListener listener)
        Specified by:
        addFilterListener in interface FilteredModel<R>
        Parameters:
        listener - a listener notified each time this model is filtered
      • refreshItems

        protected Collection<R> refreshItems()
        Returns the items this table model should contain. By default, this simply returns the items already in the model. Override to fetch data from a datasource of some kind.
        Returns:
        the items this table model should contain, an empty Collection in case of no items.
      • createColumnValueProvider

        protected <T extends NumberOptional<ColumnSummaryModel.SummaryValueProvider<T>> createColumnValueProvider​(C columnIdentifier)
        Creates a ColumnValueProvider for the given column
        Type Parameters:
        T - the value type
        Parameters:
        columnIdentifier - the column identifier
        Returns:
        a ColumnValueProvider for the column identified by the given identifier, an empty Optional if not applicable
      • addItems

        protected final void addItems​(Collection<R> items)
        Adds the given items to the bottom of this table model.
        Parameters:
        items - the items to add
      • addItemsSorted

        protected final void addItemsSorted​(Collection<R> items)
        Adds the given items to the bottom of this table model. If sorting is enabled this model is sorted after the items have been added.
        Parameters:
        items - the items to add
      • addItemsAt

        protected final void addItemsAt​(int index,
                                        Collection<R> items)
        Adds the given items to this table model, non-filtered items are added at the given index.
        Parameters:
        index - the index at which to add the items
        items - the items to add
      • addItemsAtSorted

        protected final void addItemsAtSorted​(int index,
                                              Collection<R> items)
        Adds the given items to this table model, non-filtered items are added at the given index. If sorting is enabled this model is sorted after the items have been added.
        Parameters:
        index - the index at which to add the items
        items - the items to add
        See Also:
        FilteredTableSortModel.isSortingEnabled()
      • addItem

        protected final void addItem​(R item)
        Adds the given item to the bottom of this table model.
        Parameters:
        item - the item to add
      • addItemSorted

        protected final void addItemSorted​(R item)
        Adds the given item to the bottom of this table model. If sorting is enabled this model is sorted after the item has been added.
        Parameters:
        item - the item to add
      • setItemAt

        protected final void setItemAt​(int index,
                                       R item)
        Sets the item at the given index. If the item should be filtered calling this method has no effect.
        Parameters:
        index - the index
        item - the item
        See Also:
        setIncludeCondition(Predicate)