1

I have a table view and table columns which I replaced sorting with.

Sorting Event:

nameColumn.sortTypeProperty().addListener(
new ChangeListener<SortType>() {
    @Override
    public void changed(
        ObservableValue<? extends SortType> observable,
        SortType oldValue, SortType newValue) {
            FXListSorter.sortPersonByName(fetchResults, newValue);
            showFetchResultsByPage();
        }
    });

List Sorter:

public static void sortPersonByName(ObservableList<PersonBean> list,
        SortType sortType) {
    FXCollections.sort(list, new Comparator<PersonBean>() {
        @Override
        public int compare(PersonBean p1, PersonBean p2) {
            if (sortType.equals(SortType.ASCENDING)) {
                return p1.toString().compareToIgnoreCase(p2.toString());
            } else {
                return p2.toString().compareToIgnoreCase(p1.toString());
            }
        }
    });
}

Show fetched results:

private void showFetchResultsByPage() {
    int start = currentPageIndex * pageSize;
    if (start > fetchResults.size()) {
        return;
    }
    int maxValForPage = currentPageIndex * pageSize + pageSize;
    int end = (maxValForPage <= fetchResults.size()) ? maxValForPage
            : fetchResults.size();
    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            personsTableView.getItems().setAll(
                    fetchResults.subList(start, end));
        }
    });

}

Still, when I sort the table column, the table view's default sorting is executed first, then the sorting i created.

Is there a way to disable the table view's default sorting? Thanks

1 Answer 1

8

In Java 8, to change the comparator for a column (for example to sort strings in a case-insensitive manner) you can just do

nameColumn.setComparator((name1, name2) -> name1.compareToIgnoreCase(name2));

or, equivalently but with less code:

nameColumn.setComparator(String::compareToIgnoreCase);

In general, you provide the comparator implementation for the ascending sort. This will be used directly for an ascending sort (replacing the default sort algorithm) and will be used and reversed for a descending sort.

When the sort order, sort policy, or the appropriate state of the items in the table change, the TableView's sort() method is invoked, which sorts based on the comparator associated with the columns in the sort order. To change the sort behavior, you need to override this method. Conceptually, something like

TableView<MyType> table = new TableView<MyType>() {
    @Override
    public void sort() {
        super.sort();
        showFetchResultsByPage();
    }
};

will give you what you want, but you need to be a little careful to make sure the data list is being accessed by the correct methods in the correct order (to avoid concurrent modification, and potentially infinite recursion).

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the info, I will sure put it to good use, but my problem is to cancel the sorting of the TableColumn and at the same time implement my sorting. any suggestion?
The last paragraph in James_D's response is your answer. You override the default behavior using the code he listed immediately above, then include your code after the line super.sort();
As an addendum to my previous comment, I had to remove the line super.sort(); to get it to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.