gpt4 book ai didi

java - 使用 ControlsFx CheckComboBox 过滤 TableView

转载 作者:行者123 更新时间:2023-12-02 10:50:22 26 4
gpt4 key购买 nike

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

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

这是代码,您需要一个来自 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);
}
}

最佳答案

使用CheckModel's checkedItems为此目的

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

编辑

上述绑定(bind)适用于 FilteredListpredicate 属性。由于您以不同的方式创建该绑定(bind),因此需要从该绑定(bind)返回一个非 null 对象:

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

关于java - 使用 ControlsFx CheckComboBox 过滤 TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52232150/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com