我目前正在尝试使用TableView
使用FilteredList
过滤predicate
中的数据。我有2 ComboBoxes
来过滤值。
我的表格包含Result
。每个Result
都有一个Student
,Student
有一个Classroom
。这些是我想要过滤它们的值。
当我仅使用1 ComboBox
过滤数据时,我没有任何问题。但是,我不知道如何根据两者同时过滤它。我可以提出的唯一真正的解决方案是if
changeListeners
在每个ComboBox
上都有很多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);
}
中的值会多次显示。
.checkin
答案 0 :(得分:2)
首先,不要每次都重新创建数据结构。只需更新FilteredList
上的谓词。
您可以为每个组合框创建谓词,并将每个组合框绑定到组合框中的值。然后,您可以使用Predicate.and(...)
组合谓词,并将筛选列表的谓词属性绑定到结果。
以下是一个类似的例子:
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);
}
}
答案 1 :(得分:1)
我写了一个库JFXTableView,它允许您对列进行过滤和求和。也许有人会派上用场https://github.com/Evgen2sat/JFXTableView