JavaFX FilteredList,基于列表中项目的属性进行过滤

时间:2015-06-18 12:10:57

标签: javafx javafx-8

我有一个案例需要根据项目的某些属性过滤ObservableList<Item>(即条件是内部条件而不是外部条件)。我发现javafx有FilteredList所以我试过了。我可以设置谓词和过滤工作,直到确定过滤的属性值发生变化。设置谓词现在完成如下:

list.setPredicate(t -> !t.filteredProperty().get())

由于谓词返回boolean而不是BooleanProperty,因此对该属性的更改不会反映在列表中。

这有什么简单的解决方案吗?我可以尝试做一些解决方法,例如创建一个单独的列表并同步,或者每次属性在一个项目中更改时重置谓词,希望重新触发过滤,但我首先想问一下,如果有人知道一个漂亮的解决方案,因为这些变通办法当然不是。

2 个答案:

答案 0 :(得分:7)

使用extractor创建基础列表。这将使基础列表在任何元素的filteredProperty()发生更改时触发更新事件。 FilteredList会观察这些事件,因此会相应更新:

ObservableList<Item> baseList = FXCollections.observableArrayList(item -> 
    new Observable[] {item.filteredProperty()});
FilteredList<Item> list = new FilteredList<>(baseList, t -> ! t.filteredProperty().get());

快速演示:

import java.util.stream.IntStream;

import javafx.beans.Observable;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener.Change;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;


public class DynamicFilteredListTest {

    public static void main(String[] args) {

        ObservableList<Item> baseList = FXCollections.observableArrayList(item -> 
                new Observable[] {item.filteredProperty()});

        FilteredList<Item> list = new FilteredList<>(baseList, t -> ! t.isFiltered());

        list.addListener((Change<? extends Item> c) -> {
            while (c.next()) {
                if (c.wasAdded()) {
                    System.out.println(c.getAddedSubList()+ " added to filtered list");
                }
                if (c.wasRemoved()) {
                    System.out.println(c.getRemoved()+ " removed from filtered list");
                }
            }
        });

        System.out.println("Adding ten items to base list:\n");

        IntStream.rangeClosed(1, 10).mapToObj(i -> new Item("Item "+i)).forEach(baseList::add);

        System.out.println("\nFiltered list now:\n"+list);

        System.out.println("\nSetting filtered flag for alternate items in base list:\n");

        IntStream.range(0, 5).map(i -> 2*i).mapToObj(baseList::get).forEach(i -> i.setFiltered(true));

        System.out.println("\nFiltered list now:\n"+list);
    }


    public static class Item {
        private final StringProperty name = new SimpleStringProperty() ;
        private final BooleanProperty filtered = new SimpleBooleanProperty() ;

        public Item(String name) {
            setName(name);
        }

        public final StringProperty nameProperty() {
            return this.name;
        }

        public final String getName() {
            return this.nameProperty().get();
        }

        public final void setName(final String name) {
            this.nameProperty().set(name);
        }

        public final BooleanProperty filteredProperty() {
            return this.filtered;
        }

        public final boolean isFiltered() {
            return this.filteredProperty().get();
        }

        public final void setFiltered(final boolean filtered) {
            this.filteredProperty().set(filtered);
        }

        @Override
        public String toString() {
            return getName();
        }
    }
}

答案 1 :(得分:0)

如果您正在使用数据库加载功能,需要过滤多个字段,此解决方案将有所帮助。

 ObservableList<PurchaseOrder> poData = FXCollections.observableArrayList();
 FilteredList<PurchaseOrder> filteredData;
  private void load()  {
        PurchaseOrder po = new PurchaseOrder();
        try {
            poData = po.loadTable("purchase_orders", beanFields); // Database loading data
        } catch (SQLException ex) {
            Logger.getLogger(PurchaseOrdersController.class.getName()).log(Level.SEVERE, null, ex);
        }

        filteredData = new FilteredList<>(poData, t -> true); //Encapsulate data with filter

        poTable.setItems(filteredData); //Load filtered data into table

        //Set event trigger for all filter textboxes
        txtFilter.textProperty().addListener(obs->{
            filter(filteredData);
        });
        txtFilter2.textProperty().addListener(obs->{
            filter(filteredData);
        });
  }

   private void filter(FilteredList<PurchaseOrder> filteredData)
    {
            filteredData.setPredicate(PurchaseOrder -> {
                // If all filters are empty then display all Purchase Orders
                if ((txtFilter.getText() == null || txtFilter.getText().isEmpty()) 
                        && (txtFilter2.getText() == null || txtFilter2.getText().isEmpty())) {
                    return true;
                }

                // Convert filters to lower case
                String lowerCaseFilter = txtFilter.getText().toLowerCase();
                String lowerCaseFilter2 = txtFilter2.getText().toLowerCase();

                //If fails any given criteria, fail completely
                if(txtFilter.getText().length()>0)
                    if (PurchaseOrder.get("vendor_name").toLowerCase().contains(lowerCaseFilter) == false)
                        return false;
                if(txtFilter2.getText().length()>0)
                    if (PurchaseOrder.get("PONumber").toLowerCase().contains(lowerCaseFilter2) == false)
                        return false;

                return true; // Matches given criteria
            });
    }