View Java FX Filter table

I'm trying to use a text field to filter through a table view, I want the text field (txtSearch) to look for "nhs number", "first name", "last name" and "sort category", I tried to implement various solutions on the Internet and no luck. but I'm still new to all of this, so I apologize if this was asked poorly. Any help would be greatly appreciated, my code is below.

Public class QueueTabPageController implements Initializingable {

@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);

    }

I get an error here (step 4):

(TableView.comparatorProperty());

and (step 5)

TableView.setItems(sortedData);

saying: It is not possible to make a static reference to the non-static setItems (ObservableList) method of type TableView

+4
1

TableView :

@FXML
private TableView<Patient> tableView;

....

: (tableView.comparatorProperty());

: (tableView.comparatorProperty());

: TableView.setItems(sortedData);

TableView , TableView, - patientTable!

, !

+1

All Articles