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?
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);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With