Class FilteredTableView<S>

Type Parameters:
S - The type of the objects contained within the FilteredTableView items list.
All Implemented Interfaces:
Styleable, EventTarget, Skinnable

public class FilteredTableView<S> extends TableView2<S>
A subclass of TableView2 that provides extended filtering options. The table items have to be wrapped with a FilteredList. configureForFiltering is a convenient method that can be used for that purpose.

Features


A filter icon is displayed in the column's header, and its color will show if the column has a predicate applied or not.
A PopupFilter control can be used to display filtering options. This control can be displayed via FilteredTableColumn.onFilterAction.
Alternatively, a SouthFilter control can be placed in the south header node.

Sample

Let's provide the underlying data model, based on a Person class.

 
 public class Person {
     private StringProperty firstName;
     public void setFirstName(String value) { firstNameProperty().set(value); }
     public String getFirstName() { return firstNameProperty().get(); }
     public StringProperty firstNameProperty() {
         if (firstName == null) firstName = new SimpleStringProperty(this, "firstName");
         return firstName;
     }

     private StringProperty lastName;
     public void setLastName(String value) { lastNameProperty().set(value); }
     public String getLastName() { return lastNameProperty().get(); }
     public StringProperty lastNameProperty() {
         if (lastName == null) lastName = new SimpleStringProperty(this, "lastName");
         return lastName;
     }
 }

A FilteredTableView can be created, and filled with an observable list of people, that has to be wrapped with a SortedList and a FilteredList, in order to apply sorting and filtering:

 
 FilteredTableView<Person> table = new FilteredTableView<Person>();
 ObservableList<Person> people = getPeople();
 FilteredList<Person> filteredPeople = new FilteredList<>(people);
 filteredPeople.predicateProperty().bind(table.predicateProperty());
 SortedList<Person> sortedPeople = new SortedList<>(filteredPeople);
 sortedPeople.comparatorProperty().bind(table.comparatorProperty());
 table.setItems(sortedPeople);
 

Alternatively, configureForFiltering can be used:

 
 FilteredTableView<Person> table = new FilteredTableView<Person>();
 ObservableList<Person> people = getPeople();
 FilteredTableView.configureForFiltering(table, people);
 

Now we add two columns to the table:

 
 FilteredTableColumn<Person,String> firstNameCol = new FilteredTableColumn<>("First Name");
 firstNameCol.setCellValueFactory(p -> p.getValue().firstNameProperty());
 FilteredTableColumn<Person,String> lastNameCol = new FilteredTableColumn<>("Last Name");
 lastNameCol.setCellValueFactory(p -> p.getValue().lastNameProperty());

 table.getColumns().setAll(firstNameCol, lastNameCol);

A cell factory that allows commit on focus lost can be set:

 
 firstName.setCellFactory(TextField2TableCell.forTableColumn());

We can fix some row and columns, and also show the row header:

 
 table.getFixedColumns().setAll(firstNameColumn);
 table.getFixedRows().setAll(0, 1, 2);

 table.setRowHeaderVisible(true);

A popup filter editor can be easily added to a column header:

 
 PopupFilter<Person, String> popupFirstNameFilter = new PopupStringFilter<>(firstName);
 firstName.setOnFilterAction(e -> popupFirstNameFilter.showPopup());
 

Alternatively, a south filter editor can be added to the south node:

 
 SouthFilter<Person, String> editorFirstNameFilter = new SouthFilter<>(firstName, String.class);
 firstName.setSouthNode(editorFirstNameFilter);
 
  • Property Details

    • predicate

      public final ReadOnlyObjectProperty<Predicate<S>> predicateProperty
      The predicate property is a read-only property that is representative of the current state of the filter list. The filter list contains the columns that have been added to it either programmatically or via a user clicking on the headers themselves.
      See Also:
    • filterPolicy

      public final ObjectProperty<Callback<TableView<S>,Boolean>> filterPolicyProperty
      The filter policy specifies how filtering in this FilteredTableView should be performed. For example, a basic filter policy may just call FXCollections.filter(tableView.getItems()), whereas a more advanced filter policy may call to a database to perform the necessary filtering on the server-side.

      FilteredTableView ships with a default filter policy that does precisely as mentioned above: it simply attempts to filter the items list in-place.

      It is recommended that rather than override the filter method that a different filter policy be provided instead.

      See Also:
    • onFilter

      public final ObjectProperty<EventHandler<FilterEvent<TableView<S>>>> onFilterProperty
      Called when there's a request to filter the control.
      See Also:
  • Field Details

    • DEFAULT_FILTER_POLICY

      public static final Callback<FilteredTableView,Boolean> DEFAULT_FILTER_POLICY
      The default filter policy that this FilteredTableView will use if no other policy is specified. The filter policy is a simple Callback that accepts a FilteredTableView as the sole argument and expects a Boolean response representing whether the filter succeeded (true) or not (false).
  • Constructor Details

    • FilteredTableView

      public FilteredTableView()
      Creates a FilteredTableView control.
    • FilteredTableView

      public FilteredTableView(ObservableList<S> items)
  • Method Details

    • configureForFiltering

      public static <S> void configureForFiltering(FilteredTableView<S> tableView, ObservableList<S> items)
      Convenient method to set the items for the FilteredTableView by wrapping them with a FilteredList and a SortedList, that are also bound properly to the table's predicateProperty() and TableView.comparatorProperty().
      Type Parameters:
      S - The type of the objects contained within the FilteredTableView items list
      Parameters:
      tableView - The FilteredTableView
      items - The items list
    • setBackingList

      public void setBackingList(ObservableList<S> backingList)
      Sets the original observable list, before it is wrapped into a FilteredList and a SortedList. It is required to track the changes in the underlying data model (back-end, or cell editing)
      Parameters:
      backingList - The original ObservableList
      See Also:
    • getPredicate

      public final Predicate<S> getPredicate()
      Gets the value of the property predicate.
      Property description:
      The predicate property is a read-only property that is representative of the current state of the filter list. The filter list contains the columns that have been added to it either programmatically or via a user clicking on the headers themselves.
    • predicateProperty

      public final ReadOnlyObjectProperty<Predicate<S>> predicateProperty()
      The predicate property is a read-only property that is representative of the current state of the filter list. The filter list contains the columns that have been added to it either programmatically or via a user clicking on the headers themselves.
      See Also:
    • setFilterPolicy

      public final void setFilterPolicy(Callback<TableView<S>,Boolean> callback)
      Sets the value of the property filterPolicy.
      Property description:
      The filter policy specifies how filtering in this FilteredTableView should be performed. For example, a basic filter policy may just call FXCollections.filter(tableView.getItems()), whereas a more advanced filter policy may call to a database to perform the necessary filtering on the server-side.

      FilteredTableView ships with a default filter policy that does precisely as mentioned above: it simply attempts to filter the items list in-place.

      It is recommended that rather than override the filter method that a different filter policy be provided instead.

    • getFilterPolicy

      public final Callback<TableView<S>,Boolean> getFilterPolicy()
      Gets the value of the property filterPolicy.
      Property description:
      The filter policy specifies how filtering in this FilteredTableView should be performed. For example, a basic filter policy may just call FXCollections.filter(tableView.getItems()), whereas a more advanced filter policy may call to a database to perform the necessary filtering on the server-side.

      FilteredTableView ships with a default filter policy that does precisely as mentioned above: it simply attempts to filter the items list in-place.

      It is recommended that rather than override the filter method that a different filter policy be provided instead.

    • filterPolicyProperty

      public final ObjectProperty<Callback<TableView<S>,Boolean>> filterPolicyProperty()
      The filter policy specifies how filtering in this FilteredTableView should be performed. For example, a basic filter policy may just call FXCollections.filter(tableView.getItems()), whereas a more advanced filter policy may call to a database to perform the necessary filtering on the server-side.

      FilteredTableView ships with a default filter policy that does precisely as mentioned above: it simply attempts to filter the items list in-place.

      It is recommended that rather than override the filter method that a different filter policy be provided instead.

      See Also:
    • setOnFilter

      public final void setOnFilter(EventHandler<FilterEvent<TableView<S>>> value)
      Sets the value of the property onFilter.
      Property description:
      Called when there's a request to filter the control.
    • getOnFilter

      public final EventHandler<FilterEvent<TableView<S>>> getOnFilter()
      Gets the value of the property onFilter.
      Property description:
      Called when there's a request to filter the control.
    • onFilterProperty

      public final ObjectProperty<EventHandler<FilterEvent<TableView<S>>>> onFilterProperty()
      Called when there's a request to filter the control.
      See Also:
    • resetFilter

      public void resetFilter()
      Resets all the filters applied, to both tableView and filtered columns
    • filter

      public void filter()
      The filter method forces the TableView to re-run its filtering algorithm. More often than not it is not necessary to call this method directly, as it is automatically called when the filter policy, or the state of the FilteredTableColumn filter predicate changes. In other words, this method should only be called directly when something external changes and a filter is required.