JavaFX表视图搜索过滤器不工作,但没有错误



我正在创建一个程序来存储999个报告。我试图通过三个部门来过滤这些报告:警察、医疗和消防。我试图通过过滤器搜索来做到这一点,使用TextField。我遵循了教程,但对我来说它们不起作用。

我已经检查了我的代码,试图改变一些东西,但我仍然无法让它发挥作用。我希望这里的人能知道我到底做错了什么,以及如何修复它

public class EmergencyReports extends Application {
Stage window;
TableView<Report> table;
ObservableList<Report> Reports = FXCollections.observableArrayList();
TextField dateInput, timeInput, firstNameInput, lastNameInput, locationInput, issueInput, policeInput, fireInput, medicalInput, searchInput;
public static void main(String[]args){
launch(args);
}
@Override
public void start (Stage primaryStage) throws Exception {
window = primaryStage;
window.setTitle("Emergency Operator Pannel");
//Columns created here!
TableColumn<Report,String> responseRequestedCol = new TableColumn<>("Response Requested (P/M/F)");
responseRequestedCol.setMinWidth(200);
responseRequestedCol.setCellValueFactory(new PropertyValueFactory<Report, String>("responseRequested"));
TableColumn dateCol = new TableColumn("Date (DD/MM/YY)");
dateCol.setMinWidth(25);
dateCol.setCellValueFactory(new PropertyValueFactory<Report, String>("date"));
TableColumn timeCol = new TableColumn("Time (HH:MM)");
timeCol.setMinWidth(25);
timeCol.setCellValueFactory(new PropertyValueFactory<Report, String>("time"));
TableColumn firstNameCol = new TableColumn("First Name");
firstNameCol.setMinWidth(50);
firstNameCol.setCellValueFactory(new PropertyValueFactory<Report, String>("firstName"));
TableColumn lastNameCol = new TableColumn("Last Name");
lastNameCol.setMinWidth(50);
lastNameCol.setCellValueFactory(new PropertyValueFactory<Report, String>("lastName"));
TableColumn locationCol = new TableColumn("Location");
locationCol.setMinWidth(200);
locationCol.setCellValueFactory(new PropertyValueFactory<Report, String>("location"));
TableColumn issueCol = new TableColumn("Issue");
issueCol.setMinWidth(600);
issueCol.setCellValueFactory(new PropertyValueFactory<Report, String>("issue"));
TableColumn policeCol = new TableColumn("Police");
policeCol.setMinWidth(25);
policeCol.setCellValueFactory(new PropertyValueFactory<Report, String>("police"));
TableColumn medicalCol = new TableColumn("Medical");
medicalCol.setMinWidth(25);
medicalCol.setCellValueFactory(new PropertyValueFactory<Report, String>("medical"));
TableColumn fireCol = new TableColumn("Fire");
fireCol.setMinWidth(25);
fireCol.setCellValueFactory(new PropertyValueFactory<Report, String>("fire"));
//Nested Column for emergency services      
responseRequestedCol.getColumns().addAll(policeCol, medicalCol, fireCol);
//Creation of TextFields
//Date TextFields here
searchInput = new TextField();
searchInput.setPromptText("Search Response Type");
searchInput.setMinWidth(25);
dateInput = new TextField();
dateInput.setPromptText("Date");
dateInput.setMinWidth(25);
//Time TextFields here
timeInput = new TextField();
timeInput.setPromptText("Time");
timeInput.setMinWidth(25);
//First Name TextFields here
firstNameInput = new TextField();
firstNameInput.setPromptText("First Name");
firstNameInput.setMinWidth(25);
//Last Name TextFields here
lastNameInput = new TextField();
lastNameInput.setPromptText("Last Name");
lastNameInput.setMinWidth(25);
//Location TextFields here
locationInput = new TextField();
locationInput.setPromptText("Location");
locationInput.setMinWidth(25);
//Issue TextFields here
issueInput = new TextField();
issueInput.setPromptText("Issue");
issueInput.setMinWidth(25);
//Police TextFields here
policeInput = new TextField();
policeInput.setPromptText("Police");
policeInput.setMinWidth(25);
//Fire TextFields here
fireInput = new TextField();
fireInput.setPromptText("Fire");
fireInput.setMinWidth(25);
//Medical TextFields here
medicalInput = new TextField();
medicalInput.setPromptText("Medical");
medicalInput.setMinWidth(25);
//Buttons and Lambda exoressions
Button addButton = new Button("Add");
addButton.setOnAction(e ->addButtonClicked());
Button deleteButton = new Button("Delete");
deleteButton.setOnAction(e ->deleteButtonClicked());
//Hbox layout for buttons and textfields
HBox hBox = new HBox();
HBox hBox1 = new HBox();
hBox.setPadding(new Insets(10,10,10,10));
hBox1.setSpacing(10);
hBox1.setPadding(new Insets(10,10,10,10));
hBox.setSpacing(10);
hBox.getChildren().addAll(policeInput, medicalInput, fireInput, dateInput, timeInput);
hBox1.getChildren().addAll(firstNameInput, lastNameInput, locationInput, issueInput, addButton, deleteButton, searchInput);
table = new TableView<>();
table.setItems(getReport());
table.getColumns().addAll(responseRequestedCol,dateCol,timeCol,firstNameCol,lastNameCol,locationCol,issueCol);
VBox vBox = new VBox();
vBox.getChildren().addAll(table,hBox, hBox1);

第一期

我不确定Reports字段是否已正确初始化。将其初始化为空ObservableList,但初始列表是使用getReport创建的。

第二期

将侦听器注册到相同TextFieldonKeyReleased方法内的text属性。这是不应该做的。text属性仅在文本时更新,不需要KeyEvents的处理程序。


编辑

FilteredList<Report> filteredReports = new FilteredList<>(getReport());
searchInput.textProperty().addListener((observavleValue, oldValue, newValue) -> {
if (newValue == null || newValue.isEmpty()) { // check necessary only once
filteredReports.setPredicate(null); // equivalent to setting (i->true)
} else {
final String lowerCaseFilter = newValue.toLowerCase();
filteredReports.setPredicate((Predicate<? super Report>) Report -> {
return Report.getPolice().contains(newValue)
|| Report.getMedical().toLowerCase().contains(lowerCaseFilter)
|| Report.getFire().toLowerCase().contains(lowerCaseFilter);
});
}
});
SortedList<Report> sortedReports = new SortedList<>(filteredReports);
sortedReports.comparatorProperty().bind(table.comparatorProperty());
table.setItems(sortedReports);

所有Application类都需要显示表/TextField:

@Override
public void start(Stage primaryStage) throws Exception {
TableView<Report> table = new TableView<>();

TableColumn<Report, String> policeCol = new TableColumn<>("Police");
policeCol.setCellValueFactory(new PropertyValueFactory<>("police"));
TableColumn<Report, String> medicalCol = new TableColumn<>("Medical");
medicalCol.setCellValueFactory(new PropertyValueFactory<>("medical"));
TableColumn<Report, String> fireCol = new TableColumn<>("Fire");
fireCol.setCellValueFactory(new PropertyValueFactory<>("fire"));
table.getColumns().addAll(policeCol, medicalCol, fireCol);
TextField searchInput = new TextField();
searchInput.setPromptText("Search Response Type");
// Creation of Search function created here - search via requested service
FilteredList<Report> filteredReports = new FilteredList<>(getReport());
searchInput.textProperty().addListener((observavleValue, oldValue, newValue) -> {
if (newValue == null || newValue.isEmpty()) { // check necessary only once
filteredReports.setPredicate(null); // equivalent to setting (i->true)
} else {
final String lowerCaseFilter = newValue.toLowerCase();
filteredReports.setPredicate((Predicate<? super Report>) Report -> {
return Report.getPolice().contains(newValue)
|| Report.getMedical().toLowerCase().contains(lowerCaseFilter)
|| Report.getFire().toLowerCase().contains(lowerCaseFilter);
});
}
});
SortedList<Report> sortedReports = new SortedList<>(filteredReports);
sortedReports.comparatorProperty().bind(table.comparatorProperty());
table.setItems(sortedReports);
Scene scene = new Scene(new VBox(table, searchInput));
primaryStage.setScene(scene);
primaryStage.show();
}
public ObservableList<Report> getReport() {
ObservableList<Report> Reports = FXCollections.observableArrayList();
Reports.addAll(new Report("ab", "cd", "ef"), new Report("bc", "de", "fg"));
return Reports;
}
public static class Report {
private final String fire;
private final String police;
private final String medical;
public Report(String police, String fire, String medical) {
super();
this.police = police;
this.fire = fire;
this.medical = medical;
}
public String getFire() {
return fire;
}
public String getPolice() {
return police;
}
public String getMedical() {
return medical;
}
}

您可以直接向Text属性添加侦听器,而无需侦听文本字段上的关键事件。尝试更换:

searchInput.setOnKeyReleased(e ->{
searchInput.textProperty().addListener((observavleValue, oldValue, newValue) -> {
filteredReports.setPredicate((Predicate<? super Report>) Report->{
if (newValue == null || newValue.isEmpty()){
return true;
}
String lowerCaseFilter = newValue.toLowerCase();
if(Report.getPolice().contains(newValue)){
return true;
}else if(Report.getMedical().toLowerCase().contains(lowerCaseFilter)){
return true;
}else if(Report.getFire().toLowerCase().contains(lowerCaseFilter)){
return true;
}
return false;
});
});
SortedList<Report> sortedReports = new SortedList<>(filteredReports);
sortedReports.comparatorProperty().bind(table.comparatorProperty());
table.setItems(sortedReports);
});

带有:

searchInput.textProperty().addListener((observavleValue, oldValue, newValue) -> {
filteredReports.setPredicate((Predicate<? super Report>) Report->{
if (newValue == null || newValue.isEmpty()){
return true;
}
String lowerCaseFilter = newValue.toLowerCase();
if(Report.getPolice().contains(newValue)){
return true;
}else if(Report.getMedical().toLowerCase().contains(lowerCaseFilter)){
return true;
}else if(Report.getFire().toLowerCase().contains(lowerCaseFilter)){
return true;
}
return false;
});
SortedList<Report> sortedReports = new SortedList<>(filteredReports);
sortedReports.comparatorProperty().bind(table.comparatorProperty());
table.setItems(sortedReports);
});

最新更新