使用ControlsFx CheckComboBox过滤TableView

时间:2018-09-08 04:58:56

标签: java javafx tableview javafx-8 controlsfx

我目前正在尝试使用TableView过滤CheckComboBox。作为参考,我使用的是here.的源代码,问题在于CheckComboBox的值(或属性)是ArrayList,与普通的ComboBox不同,所以我不能使用绑定+ valueProperty()等。在选择或更改某些内容时更新我的​​TableView

我想要实现的是,如果我从genderFilterCheckCombo中选择一个(或多个)性别,那么TableView将仅显示所有具有第一个性别的人。如果之后我在nameFilter上输入“ Jacob”,那么TableView现在将显示所有名称为Jacob AND 的性别为Male的人MaleFemale。如果您有更好的方法来实现这一目标,请告诉我。

这是代码,并且您需要来自here.

的ControlsFX的JAR文件

MainApp.java

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.transformation.FilteredList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import org.controlsfx.control.CheckComboBox;


public class MainApp extends Application {

    @Override
    public void start(Stage primaryStage) {
        TableView<Person> table = new TableView<>();
        table.getColumns().add(column("Name", Person::nameProperty));
        table.getColumns().add(column("Email", Person::emailProperty));
        table.getColumns().add(column("Gender", Person::genderProperty));
        table.getColumns().add(column("Birthday",Person::birthdayProperty));

        ComboBox<Person.Gender> genderFilterCombo = new ComboBox<>();
        genderFilterCombo.getItems().addAll(Person.Gender.values());

        CheckComboBox<Person.Gender> genderFilterCheckCombo= new CheckComboBox<>();
        genderFilterCheckCombo.getItems().addAll(Person.Gender.values());

        TextField nameFilterField = new TextField();
        DatePicker datefromFilterField = new DatePicker();
        DatePicker dateuntilFilterField = new DatePicker();

        ObjectProperty<Predicate<Person>> nameFilter = new SimpleObjectProperty<>();
        ObjectProperty<Predicate<Person>> genderFilter = new SimpleObjectProperty<>();
        ObjectProperty<Predicate<Person>> gender2Filter = new SimpleObjectProperty<>();
        ObjectProperty<Predicate<Person>> datefromFilter = new SimpleObjectProperty<>();
        ObjectProperty<Predicate<Person>> dateuntilFilter = new SimpleObjectProperty<>();
//      ArrayList arr = java.util.Arrays.asList(Person.Gender.values());

        nameFilter.bind(Bindings.createObjectBinding(() ->
                        person -> person.getName().toLowerCase().contains(nameFilterField.getText().toLowerCase()),
                nameFilterField.textProperty()));


        genderFilter.bind(Bindings.createObjectBinding(() ->
                        person -> genderFilterCombo.getValue() == null || genderFilterCombo.getValue() == person.getGender(),
                genderFilterCombo.valueProperty()));

        ////////////HERE//////////////
        gender2Filter.bind(Bindings.createObjectBinding(() ->
                        person -> genderFilterCheckCombo.getCheckModel().getCheckedItems().isEmpty() ||
                                genderFilterCheckCombo.getCheckModel().getCheckedItems().contains(Person.Gender.MALE)||
                                genderFilterCheckCombo.getCheckModel().getCheckedItems().contains(Person.Gender.FEMALE),
                genderFilterCheckCombo.checkModelProperty()));


        datefromFilter.bind(Bindings.createObjectBinding(() ->
                        person -> datefromFilterField.getValue() == null || datefromFilterField.getValue().isBefore(person.getBirthday())
                                || datefromFilterField.getValue().isEqual(person.getBirthday()),
                datefromFilterField.valueProperty()));

        dateuntilFilter.bind(Bindings.createObjectBinding(() ->
                        person -> dateuntilFilterField.getValue() == null || dateuntilFilterField.getValue().isAfter(person.getBirthday())
                                || dateuntilFilterField.getValue().isEqual(person.getBirthday()),
                dateuntilFilterField.valueProperty()));

        FilteredList<Person> filteredItems = new FilteredList<>(FXCollections.observableList(createData()));
        table.setItems(filteredItems);

        filteredItems.predicateProperty().bind(Bindings.createObjectBinding(
                () -> nameFilter.get().and(genderFilter.get().and(gender2Filter.get().and(datefromFilter.get().and(dateuntilFilter.get())))),
                nameFilter, genderFilter, gender2Filter, datefromFilter, dateuntilFilter));

        Button clear = new Button("Clear Filters");
        clear.setOnAction(e -> {
            genderFilterCombo.setValue(null);
 //         genderFilterCheckCombo.getCheckModel().clearChecks();
            System.out.println(genderFilterCombo.valueProperty());
            System.out.println(genderFilterCheckCombo.checkModelProperty());
            System.out.println(genderFilterCheckCombo.getCheckModel().getCheckedItems());
            System.out.println(genderFilterCheckCombo.getCheckModel().getCheckedItems().isEmpty());
            System.out.println(genderFilterCheckCombo.getCheckModel().getCheckedItems().contains(Person.Gender.MALE) ||
                    genderFilterCheckCombo.getCheckModel().getCheckedItems().contains(Person.Gender.FEMALE) );
            nameFilterField.clear();
            datefromFilterField.setValue(null);
            dateuntilFilterField.setValue(null);
        });

        HBox filters = new HBox(5, nameFilterField, genderFilterCombo, datefromFilterField, dateuntilFilterField, genderFilterCheckCombo, clear);
        filters.setPadding(new Insets(5));
        BorderPane root = new BorderPane(table, filters, null, null, null);
        Scene scene = new Scene(root, 800, 600);


        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private List<Person> createData() {
        return Arrays.asList(
                new Person("Jacob Smith", "jacob.smith@example.com", Person.Gender.MALE, LocalDate.parse("2018-08-25")),
                new Person("Jacob Smith", "jacob.smith@example.com", Person.Gender.MALE, LocalDate.parse("2018-08-26")),
                new Person("Jacob Smith", "jacob.smith@example.com", Person.Gender.FEMALE, LocalDate.parse("2018-08-26")),
                new Person("Jacob Smith", "jacob.smith@example.com", Person.Gender.MALE, LocalDate.parse("2018-08-25")),
                new Person("Isabella Johnson", "isabella.johnson@example.com", Person.Gender.FEMALE, LocalDate.parse("2018-08-21")),
                new Person("Ethan Williams", "ethan.williams@example.com", Person.Gender.MALE, LocalDate.parse("2018-08-21")),
                new Person("Emma Jones", "emma.jones@example.com", Person.Gender.FEMALE, LocalDate.parse("2018-08-29")),
                new Person("Michael Brown", "michael.brown@example.com", Person.Gender.MALE, LocalDate.parse("2018-08-05"))
        );
    }

    private static <S,T> TableColumn<S,T> column(String title, Function<S, ObservableValue<T>> property) {
        TableColumn<S,T> col = new TableColumn<>(title);
        col.setCellValueFactory(cellData -> property.apply(cellData.getValue()));
        return col ;
    }



    public static class Person {
 //     List<Gender> abc = Arrays.asList(Person.Gender.values());
        public enum Gender {MALE, FEMALE }

        private final StringProperty name = new SimpleStringProperty();
        private final StringProperty email = new SimpleStringProperty() ;
        private final ObjectProperty<Gender> gender = new SimpleObjectProperty<>();
        private final ObjectProperty<LocalDate> birthday = new SimpleObjectProperty<>();




        public Person(String name, String email, Gender gender, LocalDate birthday) {
            setName(name);
            setEmail(email);
            setGender(gender);
            setBirthday(birthday);
        }

        public final ObjectProperty<LocalDate> birthdayProperty(){
            return this.birthday;
        }

        public final LocalDate getBirthday() {
            return this.birthdayProperty().get();
        }

        public final void setBirthday(final LocalDate birthday) {
            this.birthdayProperty().set(birthday);
        }

        public final StringProperty emailProperty() {
            return this.email;
        }

        public final String getEmail() {
            return this.emailProperty().get();
        }

        public final void setEmail(final String email) {
            this.emailProperty().set(email);
        }

        public final ObjectProperty<Gender> genderProperty() {
            return this.gender;
        }

        public final Gender getGender() {
            return this.genderProperty().get();
        }

        public final void setGender(final Gender gender) {
            this.genderProperty().set(gender);
        }

        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 static void main(String[] args) {
        launch(args);
    }
}

1 个答案:

答案 0 :(得分:0)

为此目的使用CheckModel's checkedItems

gender2Filter.bind(Bindings.createObjectBinding(() -> genderFilterCheckCombo.getCheckModel().getCheckedItems().isEmpty()
                        ? null
                        : person -> genderFilterCheckCombo.getCheckModel().getCheckedItems().contains(person.getGender()),
        genderFilterCheckCombo.getCheckModel().getCheckedItems()));

编辑

以上绑定将适用于predicate的{​​{1}}属性。由于您以其他方式创建绑定,因此需要从绑定中返回非FilteredList对象:

null