Samarek
Samarek

Reputation: 444

Sorting the columns of a JavaFX TableView

Now I have been searching for quite a while and all I find are people trying to sort the TableRows through a TableColumn, but I want to sort the TableColumns alphabetically.

Say I have a Table with the columns 'Name', 'Pain' and 'Gain' and after sorting I would like it to be 'Gain', 'Name' and 'Pain'.

Can't really figure out a way to do this, but it can't be that hard, can it ?

... I wanna do it programmatically of course and not drag the columns around.

Upvotes: 0

Views: 604

Answers (1)

Matthew McPeak
Matthew McPeak

Reputation: 17944

Sorry I don't have time to post code right now, but here is a method that should work:

  • Create a Comparator<TableColumn> that will sort by column name.
  • Apply the sort by myTableView.getColumns().sort(myComparator)

Where myTableView is the name of your TableView and myComparator is the name of the Comparator<TableColumn> you created.

Upvotes: 1

Related Questions