Java FX筛选表视图

4
我正在尝试使用文本字段来过滤表格视图,我想要一个文本字段(txtSearch)来搜索“nhs号码”,“名字”,“姓氏”和“分诊类别”。我已经尝试了在线上的各种解决方案,但都没有成功。由于我还是新手,如果我的问题提得不好,请见谅。任何帮助都将不胜感激,下面是我的代码。 public class QueueTabPageController implements Initializable {
@FXML
private TableView<Patient> tableView;

@FXML
private TableColumn<Patient, String> NHSNumberColumn;

@FXML
private TableColumn<Patient, String> firstNameColumn;

@FXML
private TableColumn<Patient, String> lastNameColumn;

@FXML
private TableColumn<Patient, String> timeEnteredColumn;

@FXML
private TableColumn<Patient, String> triageAssessmentColumn;

@FXML
private TextField filterField;

@FXML
private QueueTabPageController queueTabPageController;

private ObservableList<Patient> tableData;

// public static LinkedList<Patient> displayQueue;


/**
     * Initializes the controller class. This method is automatically called
     * after the fxml file has been loaded.
     * 
     * Initializes the table columns and sets up sorting and filtering.
     */
    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {

        assert tableView != null : "fx:id=\"tableView\" was not injected: check your FXML file 'FXMLQueueTabPage.fxml'";

        NHSNumberColumn.setCellValueFactory(new PropertyValueFactory<Patient, String>("nhsNumber"));
        firstNameColumn.setCellValueFactory(new PropertyValueFactory<Patient, String>("firstName"));
        lastNameColumn.setCellValueFactory(new PropertyValueFactory<Patient, String>("lastName"));
        timeEnteredColumn.setCellValueFactory(new PropertyValueFactory<Patient, String>("timeEnteredString"));
        triageAssessmentColumn.setCellValueFactory(new PropertyValueFactory<Patient, String>("triage"));

        // display the current queue to screen when opening page each time
        displayQueue(Queue.queue);

        // 0. Initialize the columns.
        //firstNameColumn.setCellValueFactory(new PropertyValueFactory<Patient, String>("firstName"));
        //lastNameColumn.setCellValueFactory(new PropertyValueFactory<Patient, String>("lastName"));

        // 1. Wrap the ObservableList in a FilteredList (initially display all
        // data).
        FilteredList<Patient> filteredData = new FilteredList<>(tableData,
                p -> true);

        // 2. Set the filter Predicate whenever the filter changes.
        filterField.textProperty().addListener(
                (observable, oldValue, newValue) -> {
                    filteredData.setPredicate(Patient -> {
                        // If filter text is empty, display all persons.
                            if (newValue == null || newValue.isEmpty()) {
                                return true;
                            }

                            // Compare first name and last name of every person
                            // with filter text.
                            String lowerCaseFilter = newValue.toLowerCase();

                            if (Patient.getFirstName().toLowerCase()
                                    .contains(lowerCaseFilter)) {
                                return true; // Filter matches first name.
                            } else if (Patient.getLastName().toLowerCase()
                                    .contains(lowerCaseFilter)) {
                                return true; // Filter matches last name.
                            }
                            return false; // Does not match.
                        });
                });

        // 3. Wrap the FilteredList in a SortedList.
        SortedList<Patient> sortedData = new SortedList<>(filteredData);

        // 4. Bind the SortedList comparator to the TableView comparator.
        sortedData.comparatorProperty().bind(tableView.comparatorProperty());

        // 5. Add sorted (and filtered) data to the table.
        tableView.setItems(sortedData);

    }

我在这里遇到了一个错误(第4步):
(TableView.comparatorProperty());

并且(第五步)

TableView.setItems(sortedData);

提示信息:

无法对 TableView 类型中的非静态方法 setItems(ObservableList) 进行静态引用


我尝试了在线上各种解决方案,但都没有成功。请展示你所尝试的内容,并解释出了什么问题。 - James_D
@James_D 我主要尝试了这个教程:http://code.makery.ch/blog/javafx-8-tableview-sorting-filtering 但是当我在文本字段中输入时,它从未触发。 - Davidaj
那个教程中的代码在我的电脑上运行良好。 - James_D
@James_D,我好像无法将它实现到我的代码中,能帮忙吗? - Davidaj
只要您仅操作 tableData,并且从未在其他任何地方调用过 tableView.setItems(...),那么这不会有任何影响。 - James_D
显示剩余2条评论
1个回答

1
你的 TableView 被定义为:
@FXML
private TableView<Patient> tableView;

所以....

尝试使用:(tableView.comparatorProperty());

而不是:(TableView.comparatorProperty());

并且对于下面的代码也做同样的更改:TableView.setItems(sortedData);

我建议你将tableView更改为其他名称,与TableView不同,例如patientTable

让我知道是否有效!


已修复错误,没有出现错误,但仍未触发,可能是因为表最初为空,直到添加患者为止?所以它不是预填充的吗?抱歉,对这一切都很新。 - Davidaj

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接