springspring-bootjavafx

Filtering a tableview


I have a program that uses javafx and springboot. I have a table that has two columns - a code column and a description column. The end user can type within a search textfield and the column filters based on what text is being typed.

Previously I was able to use the FilterList function to use for the filtering however since implementing springboot I haven't been able to get this to work properly.

DiagnosisCode.java

@Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

        codeCol.setCellValueFactory((new PropertyValueFactory<>("Code")));
        descripCol.setCellValueFactory((new PropertyValueFactory<>("Description")));
        newDiagCodeTable.getColumns().setAll(codeCol, descripCol);
        newDiagCodeTable.setItems(FXCollections.observableArrayList(diagnosisService.findAll()));
        sortData();

    }

   @FXML
    public void sortData(){
        FilteredList<IDiagnosisModel> filteredData = new FilteredList<>(listView, b -> true);
        searchCodes.textProperty().addListener((obs, oldValue, newValue) -> {
            filteredData.setPredicate(diagnosisCode -> {
                if (newValue == null || newValue.isEmpty()) {
                    return true;
                }
                String lowerCaseFilter = newValue.toLowerCase();

                if (diagnosisCode.getDiagnosisDescription().toLowerCase().contains(lowerCaseFilter)) {
                    return true;
                } else if (diagnosisCode.getDiagnosisCode().toLowerCase().contains(lowerCaseFilter)) {
                    return true;

                } else return false;
            });
        });
        SortedList<IDiagnosisModel> sortedData = new SortedList<>(filteredData);
        sortedData.comparatorProperty().bind(newDiagCodeTable.comparatorProperty());
        newDiagCodeTable.setItems(sortedData);
    }

I have tried to use the findByCode(String code). For example I had setup a setOnKeyPressed event that would get the text of what was typed in and use the findByCode() function to take in the String of what has been typed. This didn't work.

Is there a springboot specific method that I am missing?

DiagnosisCode.java

  @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

        sortData();

        codeCol.setCellValueFactory((new PropertyValueFactory<>("Code")));
        descripCol.setCellValueFactory((new PropertyValueFactory<>("Description")));
        newDiagCodeTable.getColumns().setAll(codeCol, descripCol);
        newDiagCodeTable.setItems(FXCollections.observableArrayList(diagnosisService.findAll()));

        this.searchCodes.setOnKeyPressed(keyEvent -> {
            String name = this.searchCodes.getText().trim();
            diagnosisService.findByCode(name);
        });


    }

Solution

  • This was a logic error.

    Below is the solution:

    codeCol.setCellValueFactory((new PropertyValueFactory<>("Code")));
        descripCol.setCellValueFactory((new PropertyValueFactory<>("Description")));
        newDiagCodeTable.getColumns().setAll(codeCol, descripCol);
    
        listView.addAll(diagnosisService.findAll());
        newDiagCodeTable.setItems(listView);
    
    
        FilteredList<IDiagnosisModel> filteredData = new FilteredList<>(listView, b -> true);
        searchCodes.textProperty().addListener((obs, oldValue, newValue) -> {
            filteredData.setPredicate(diagnosisCode -> {
                if (newValue == null || newValue.isEmpty()) {
                    return true;
                }
                String lowerCaseFilter = newValue.toLowerCase();
    
                if (diagnosisCode.getDiagnosisDescription().toLowerCase().contains(lowerCaseFilter)) {
                    return true;
                } else if (diagnosisCode.getDiagnosisCode().toLowerCase().contains(lowerCaseFilter)) {
                    return true;
    
                } else return false;
            });
        });
        SortedList<IDiagnosisModel> sortedData = new SortedList<>(filteredData);
        sortedData.comparatorProperty().bind(newDiagCodeTable.comparatorProperty());
        newDiagCodeTable.setItems(sortedData);
    

    I created listview to hold the codes found within the findAll() function, then created the filter and sort and added it to the table.