gpt4 book ai didi

javafx - 使用多个值过滤 JFX TableView

转载 作者:行者123 更新时间:2023-12-05 08:56:10 32 4
gpt4 key购买 nike

我目前正在尝试使用带有 predicateFilteredList 来过滤我的 TableView 中的数据。我有 2 个 ComboBoxes 来过滤值。

我的表包含结果。每个 Result 都有一个 Student,那个 Student 有一个他所属的 Classroom。这些是我想要的值过滤它们。

当我仅使用 1 个 ComboBox 过滤数据时,我没有遇到任何问题。但是,我不知道如何同时基于两者进行过滤。我能想出的唯一真正的解决方案是在每个 ComboBoxchangeListeners 中都有很多 if 语句,这似乎是一种不必要的复杂方式去做。

如果我使用以下代码,我的 TableView 中的值会显示多次。

  private void filterData(){
ObservableList<Result> observableList = FXCollections.observableList(view.getResultTableView().getAllResultsList());
FilteredList<Result> filteredData = new FilteredList<>(observableList, p -> true);

view.getClassroomComboBox().getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {

filteredData.setPredicate(result -> {

if (newValue.equals(view.getAllClassroomsChoice()) || newValue.equals(result.getStudent().getClassroom())) {
return true;
}
else
return false;
});

if(newValue.equals(view.getAllClassroomsChoice())){
view.getStudentComboBox().setAllStudents();
}
else{
view.getStudentComboBox().setStudentsByClassroom((Classroom) newValue);
}
});

view.getStudentComboBox().getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
filteredData.setPredicate(result -> {

if (newValue.equals(view.getAllClassroomsChoice()) || newValue.equals(result.getStudent())) {
return true;
}
else
return false;
});
});


SortedList<Result> sortedData = new SortedList<>(filteredData);

sortedData.comparatorProperty().bind(view.getResultTableView().comparatorProperty());

view.getResultTableView().setItems(sortedData);

}

最佳答案

首先,不要每次都重新创建数据结构。只需更新 FilteredList 上的谓词。

您可以为每个组合框创建谓词并将每个谓词绑定(bind)到组合框中的值。然后您可以使用 Predicate.and(...) 组合谓词并将筛选列表的谓词属性绑定(bind)到结果。

这是一个类似的例子:

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.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class MultipleFilterTableExample 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));

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

TextField nameFilterField = new TextField();

ObjectProperty<Predicate<Person>> nameFilter = new SimpleObjectProperty<>();
ObjectProperty<Predicate<Person>> genderFilter = new SimpleObjectProperty<>();

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()));

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

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

Button clear = new Button("Clear Filters");
clear.setOnAction(e -> {
genderFilterCombo.setValue(null);
nameFilterField.clear();
});

HBox filters = new HBox(5, nameFilterField, genderFilterCombo, clear);
filters.setPadding(new Insets(5));
BorderPane root = new BorderPane(table, filters, null, null, null);
Scene scene = new Scene(root, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}

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

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 {

public enum Gender {MALE, FEMALE }

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

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

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);
}
}

关于javafx - 使用多个值过滤 JFX TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42138867/

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