vendredi 29 novembre 2019

JavaFX sorting tableview - issues using comparator and updating the tableview to the sorted list

I'm experiencing a lot of errors trying to sort my TableView in JavaFX. The view consists of the table, and two check boxes, sorting a view by status (alphabetical) and a referralDate (numeric).

public class ReferralListCtrl implements Initializable {

    @FXML
    private TableView<ReferralListModel> referralListTable;
    @FXML
    private TableColumn<ReferralListModel, String> recievedDateColumn;
    @FXML
    private TableColumn<ReferralListModel, String> referredDateColumn;
    @FXML
    private TableColumn<ReferralListModel, String> layDaysColumn;
    @FXML
    private TableColumn<ReferralListModel, String> statusColumn;
    @FXML
    private TableColumn<ReferralListModel, String> assignedColumn;
    @FXML
    private TableColumn<ReferralListModel, String> referredFromColumn;
    @FXML
    private TableColumn<ReferralListModel, String> patientColumn;
    @FXML
    private TableColumn<ReferralListModel, String> referralCauseColumn;
    @FXML
    private TableColumn<ReferralListModel, String> referralIDColumn;
    @FXML
    private TableColumn<ReferralListModel, String> referralTypeColumn;
    @FXML
    private CheckBox sortByDateBtn;
    @FXML
    private CheckBox sortByStatusBtn;
    @FXML
    private Label searchLabelTest;
    @FXML
    private HBox sortHBox;

    //If button is pressed
    @FXML
    public void searchButtonPressed(){

            String sortLabel = "Valgte sortering:";
            if (sortByDateBtn.isSelected()){
                //control to see if buttons is detectable
                sortLabel += "\nDato";
            }

            if (sortByStatusBtn.isSelected()){
                SortedList<String> mySortedList = new SortedList(getReferralListData());
                mySortedList.setComparator(new Comparator<ReferralListModel>(){
                    @Override
                    public int compare(ReferralListModel o1, ReferralListModel o2) {
                        //Two methods of comparing I have tried
                        return o1.compareToIgnoreCase(o2);
                        //return o1.getStatus().compareToIgnoreCase(o2.getStatus());
                    }
                });
                //updating the list
                referralListTable.setItems(referralListData.sorted());

                sortLabel += "\nstatus";
            }

        this.searchLabelTest.setText(sortLabel);
    } 

    // Reference to the main application.
    private MainApp mainApp;

    /***
     * The constructor. The constructor is called before the initialize() method.
     */
    public ReferralListCtrl() {
    }

    //defining new lists that is sorted
private SortedList mySortedList;
private ObservableList<Comparator<ReferralListModel>> listComparators;
ObservableList<ReferralListModel> referralListData = FXCollections.observableArrayList();


/**
 * Returns the data as an observable list of the referrals.
 * 
 * @return
 */
public ObservableList<ReferralListModel> getReferralListData() {
   return referralListData;
}



    @Override
    public void initialize(URL location, ResourceBundle resources) {
        recievedDateColumn.setCellValueFactory(new PropertyValueFactory<ReferralListModel, String>("recievedDate"));
        referredDateColumn.setCellValueFactory(new PropertyValueFactory<ReferralListModel, String>("referredDate"));
        layDaysColumn.setCellValueFactory(new PropertyValueFactory<ReferralListModel, String>("layDays"));
        statusColumn.setCellValueFactory(new PropertyValueFactory<ReferralListModel, String>("status"));
        assignedColumn.setCellValueFactory(new PropertyValueFactory<ReferralListModel, String>("assigned"));
        referredFromColumn.setCellValueFactory(new PropertyValueFactory<ReferralListModel, String>("referredFrom"));
        patientColumn.setCellValueFactory(new PropertyValueFactory<ReferralListModel, String>("patient"));
        referralCauseColumn.setCellValueFactory(new PropertyValueFactory<ReferralListModel, String>("referralCause"));
        referralIDColumn.setCellValueFactory(new PropertyValueFactory<ReferralListModel, String>("referralID"));
        referralTypeColumn.setCellValueFactory(new PropertyValueFactory<ReferralListModel, String>("referralType"));

        referralListData.add(new ReferralListModel("1/1-2019", "1/12-2019", "30", "Modtaget", "Alle", "Laegehus A",
        "Hans 233492-1233", "Aarsag 1", "ICPC-kode", "A"));
referralListData.add(new ReferralListModel("12/1-2019", "12/1-2019", "0", "Visiteret", "Alle", "Laegehus B",
        "Ruth 290506-1236", "Aarsag 1", "ICD10-kode", "B"));
referralListData.add(new ReferralListModel("2/1-2019", "2/1-2019", "29", "Modtaget", "Alle", "Laegehus C",
        "Heinz 311200-9561", "Aarsag 1", "ICPC-kode", "C"));
referralListData.add(new ReferralListModel("4/1-2019", "1/1-2019", "27", "Modtaget", "Alle", "Laegehus D",
        "Cornelia 290483-2096", "Aarsag 1", "ICPC-kode", "D"));
referralListData.add(new ReferralListModel("7/1-2019", "6/1-2019", "24", "Modtaget", "Alle", "Laegehus E",
        "Werner 192835-1023", "Aarsag 1", "ICPC-kode", "E"));
referralListData.add(new ReferralListModel("20/1-2019", "19/1-2019", "0", "Visiteret", "Alle", "Laegehus F",
        "Lydia 101039-5302", "Aarsag 1", "ICD10-kode", "F"));

        referralListTable.setItems(getReferralListData());
    }

How do I modify the comparator so that it will sort alphabeticly and numericly?

How to I update the tableview to the sorted list?




Aucun commentaire:

Enregistrer un commentaire