Java FX 筛选器表视图



我正在尝试使用文本字段来过滤表格视图,我想要一个文本字段(txtSearch)来搜索"nhs号码","名字","姓氏"和"分类类别"。我尝试过在线实施各种解决方案,但没有运气,我仍然对这一切很陌生,所以如果这个问题问得不好,我深表歉意。任何帮助将不胜感激,我的代码如下。

public class QueueTabPageController 实现 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());

和(步骤 5)

TableView.setItems(sortedData);

谚语:无法从 TableView 类型对非静态方法 setItems(ObservableList) 进行静态引用

您的TableView定义为:

@FXML
private TableView<Patient> tableView;

所以。。。。

尝试输入:(tableView.comparatorProperty());

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

并执行相同的操作:TableView.setItems(sortedData);

我建议你把tableView换成其他比TableView更不同的,比如patientTable

让我知道它是否有效!

最新更新