Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX: Disabling TableView multi-column sorting

I need my TableView to have all its columns sortable, but I do not want it to be multi-column sortable through shift-clicking the columns. Is there any way to prevent that from happening?

like image 202
Jai Avatar asked Mar 14 '26 10:03

Jai


1 Answers

You can add a listener to the sortOrder property of your TableView, which is an ObservableList, and check that this list never has more than one entry:

myTableView.getSortOrder().addListener((ListChangeListener.Change<? extends TableColumn> c) -> {
    while (myTableView.getSortOrder().size() > 1) {
        myTableView.getSortOrder().remove(1);
    }
});

However, note that this approach invalidates the Change object for all subsequent listeners. Therefore, if you have other listeners for the sortOrder property, you might want to use this approach:

myTableView.setOnSort(sortEvent -> {
    while (myTableView.getSortOrder().size() > 1) {
        myTableView.getSortOrder().remove(1);
    }
});
like image 164
user7291698 Avatar answered Mar 15 '26 22:03

user7291698



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!