JavaFX tableview搜索过滤器不起作用,但是没有错误?

时间:2018-12-17 18:39:32

标签: java javafx tableview

我正在创建一个程序来存储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);

2 个答案:

答案 0 :(得分:0)

您可以直接将侦听器添加到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);
});

答案 1 :(得分:0)

第一期

我不确定Step 1/2 : FROM ubuntu:14.04 ERROR: Service 'trusty' failed to build: Get https://registry-1.docker.io/v2/: dial tcp: lookup registry-1.docker.io: no such host 字段是否已正确初始化。将其初始化为空的docker run -it --rm ubuntu:14.04 bash,但是初始列表是使用Reports创建的。

第二期

对于相同的ObservableList,您可以在getReport方法内的text属性中注册侦听器。不应该这样做。 onKeyReleased属性仅在文本时更新,不需要TextField的处理程序。


编辑

text

所有KeyEvent类都需要显示表/ 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