Java FX过滤器表视图 [英] Java FX Filter table view

查看:138
本文介绍了Java FX过滤器表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用文本字段来过滤表格视图,我想要一个文本字段(txtSearch)来搜索'nhs number','first name','last name'和'triage category' 。我已经尝试在线实施各种解决方案并且没有运气,我仍然对这一切都不熟悉,如果这被问得很糟糕,那么道歉。任何帮助将不胜感激,我的代码如下。

I'm trying to use a text field to filter through a table view, I want a text field (txtSearch) to search for the 'nhs number', 'first name', 'last name' and 'triage category'. I've tried implementing various solutions online and had no luck, I'm still new to all this so apologies if this was asked poorly. any help would be greatly appreciated, my code is below.

公共类QueueTabPageController实现Initializable {

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):

I'm getting an error here (step 4):

(TableView.comparatorProperty());

和(步骤5)

TableView.setItems(sortedData);

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

saying: Cannot make a static reference to the non-static method setItems(ObservableList) from the type TableView

推荐答案

您的 TableView 定义为:

@FXML
private TableView<Patient> tableView;

所以....

尝试放:(tableView.comparatorProperty());

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

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

我建议您更改 tableView 以获取更多不同于的TableView ,类似 patientTable

I recommend you to change tableView for other more different than TableView, something like patientTable!

让我知道它是否有效!

这篇关于Java FX过滤器表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆