JavaFX tableview搜索筛选器不工作,但没有错误?

JavaFX tableview搜索筛选器不工作,但没有错误?,java,javafx,tableview,Java,Javafx,Tableview,我正在创建一个程序来存储999份报告。我试着从三个方面筛选这些报告:警察、医疗和消防。我正试图通过一个过滤器搜索,使用一个文本字段来实现这一点。我已经学习了教程,但是对我来说它们不起作用 我已经检查了我的代码,试图改变一些东西,但我仍然无法让它工作。我希望这里的人可能知道我到底做错了什么,以及如何纠正它 public class EmergencyReports extends Application { Stage window; TableView<Report>

我正在创建一个程序来存储999份报告。我试着从三个方面筛选这些报告:警察、医疗和消防。我正试图通过一个过滤器搜索,使用一个文本字段来实现这一点。我已经学习了教程,但是对我来说它们不起作用

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

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);
公共类EmergencyReports扩展了应用程序{
舞台窗口;
表视图表;
ObservableList Reports=FXCollections.observableArrayList();
TextField dateInput、timeInput、firstNameInput、lastNameInput、locationInput、issueInput、PolicyInput、fireInput、medicalInput、searchInput;
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
public void start(Stage primaryStage)引发异常{
窗口=初级阶段;
窗口设置标题(“紧急操作员面板”);
//在这里创建的列!
TableColumn responseRequestedCol=新的TableColumn(“请求的响应(P/M/F)”);
responseRequestedCol.setMinWidth(200);
responseRequestedCol.setCellValueFactory(新属性ValueFactory(“responseRequested”));
TableColumn dateCol=新的TableColumn(“日期(DD/MM/YY)”);
dateCol.setMinWidth(25);
dateCol.setCellValueFactory(新属性ValueFactory(“日期”);
TableColumn timeCol=新的TableColumn(“时间(HH:MM)”);
timeCol.setMinWidth(25);
timeCol.setCellValueFactory(新属性ValueFactory(“时间”));
TableColumn firstNameCol=新的TableColumn(“名字”);
firstNameCol.setMinWidth(50);
firstNameCol.setCellValueFactory(新属性ValueFactory(“firstName”));
TableColumn lastNameCol=新的TableColumn(“姓氏”);
lastNameCol.setMinWidth(50);
lastNameCol.setCellValueFactory(新属性ValueFactory(“lastName”));
TableColumn locationCol=新的TableColumn(“位置”);
位置坐标设置最小宽度(200);
locationCol.setCellValueFactory(新属性ValueFactory(“位置”);
TableColumn issueCol=新的TableColumn(“发行”);
issueCol.setMinWidth(600);
发行eCol.setCellValueFactory(新财产价值工厂(“发行”);
TableColumn policicol=新的TableColumn(“警察”);
policcol.setMinWidth(25);
Policicol.setCellValueFactory(新财产价值工厂(“警察”);
TableColumn medicalCol=新的TableColumn(“医疗”);
医疗设置最小宽度(25);
medicalCol.setCellValueFactory(新财产价值工厂(“医疗”);
TableColumn fireCol=新的TableColumn(“火灾”);
火柱设置最小宽度(25);
fireCol.setCellValueFactory(新属性价值工厂(“fire”));
//应急服务用嵌套柱
responseRequestedCol.getColumns().addAll(policycol、medicalCol、fireCol);
//文本字段的创建
//此处的日期文本字段
searchInput=新文本字段();
searchInput.setPrompText(“搜索响应类型”);
searchInput.setMinWidth(25);
dateInput=新文本字段();
dateInput.SetPrompText(“日期”);
dateInput.setMinWidth(25);
//时间文本字段在这里
timeInput=新文本字段();
timeInput.SetPrompText(“时间”);
时间输入。设置最小宽度(25);
//名字文本字段在这里
firstNameInput=newtextfield();
firstNameInput.setpromptext(“First Name”);
firstNameInput.setMinWidth(25);
//此处的姓氏文本字段
lastNameInput=新文本字段();
setPrompText(“姓氏”);
lastNameInput.setMinWidth(25);
//位置文本字段在这里
locationInput=新文本字段();
locationInput.SetPrompText(“位置”);
locationInput.setMinWidth(25);
//在此处发布文本字段
issueInput=新文本字段();
issueInput.SetPrompText(“发行”);
发布输入设置最小宽度(25);
//这里有警察文本字段
policyInput=新文本字段();
policInput.setPrompText(“警察”);
policInput.setMinWidth(25);
//在这里开火
fireInput=新文本字段();
fireInput.SetPrompText(“火灾”);
fireInput.setMinWidth(25);
//这里是医学文本字段
medicalInput=新文本字段();
medicalInput.SetPrompText(“医疗”);
medicalInput.setMinWidth(25);
//纽扣和Lambda外饰
按钮添加按钮=新按钮(“添加”);
设置操作(e->addButtonClicked());
按钮删除按钮=新按钮(“删除”);
setOnAction(e->deleteButtonClicked());
//按钮和文本字段的Hbox布局
HBox HBox=新的HBox();
HBox hBox1=新的HBox();
hBox.设置填充(新插图(10,10,10,10));
hBox1.setspace(10);
hBox1.设置填充(新插图(10,10,10,10));
hBox.setspace(10);
hBox.getChildren().addAll(policyInput、medicalInput、fireInput、dateInput、timeInput);
hBox1.getChildren().addAll(firstNameInput、lastNameInput、locationInput、issueInput、addButton、deleteButton、searchInput);
table=新的TableView();
table.setItems(getReport());
table.getColumns().addAll(responseRequestedCol、dateCol、timeCol、firstNameCol、lastNameCol、locationCol、issueCol);
VBox VBox=新的VBox();
vBox.getChildren().addAll(表,hBox,hBox1);

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

 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.setOnKeyReleased(e->{
searchInput.textProperty().addListener((observavleValue、oldValue、newValue)->{
filteredReports.setPredicate((PredicateFirst issue
我不确定
报告
字段是否正确初始化。Initi
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);
});
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);
@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;
    }

}