Sort TableView by certain column Javafx

Viewed 17844

I'm trying to sort a TableView table by a certain column. When I display the table, it's shown but from what I'm finding on here and other sites it's just confusing me more and more.

here is the tableview code

public TableView<Animal> animalLostLocation(ArrayList<Animal> list)
    {

        TableColumn<Animal, String> atypeColumn = new TableColumn<>("Animal Type");
        atypeColumn.setMinWidth(200);
        atypeColumn.setSortable(false);
        atypeColumn.setCellValueFactory(new PropertyValueFactory<>("aType"));

        TableColumn<Animal, String> descriptionColumn = new TableColumn<>("Description");
        descriptionColumn.setMinWidth(200);
        descriptionColumn.setSortable(false);
        descriptionColumn.setCellValueFactory(new PropertyValueFactory<>("description"));

        TableColumn<Animal, String> breedColumn = new TableColumn<>("Breed");
        breedColumn.setMinWidth(80);
        breedColumn.setSortable(false);
        breedColumn.setCellValueFactory(new PropertyValueFactory<>("breed"));

        TableColumn<Animal, String> nameColumn = new TableColumn<>("Name");
        nameColumn.setMinWidth(200);
        nameColumn.setSortable(false);
        nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));

        TableColumn<Animal, Catergory> catColumn = new TableColumn<>("Category");
        catColumn.setMinWidth(200);
        breedColumn.setSortable(false);
        catColumn.setCellValueFactory(new PropertyValueFactory<>("category"));

        TableColumn<Animal, Integer > ageColumn = new TableColumn<>("Age");
        ageColumn.setMinWidth(50);
        ageColumn.setSortable(false);
        ageColumn.setCellValueFactory(new PropertyValueFactory<>("age"));

        TableColumn<Animal, Integer > idColumn = new TableColumn<>("ID");
        idColumn.setMinWidth(50);
        idColumn.setCellValueFactory(new PropertyValueFactory<>("ID"));

        TableColumn<Animal, Boolean > genColumn = new TableColumn<>("Male");
        genColumn.setMinWidth(50);
        genColumn.setSortable(false);
        genColumn.setCellValueFactory(new PropertyValueFactory<>("gender"));

        table = new TableView<Animal>();

        table.setItems(getAnimal(list));

        table.getColumns().addAll(genColumn, idColumn, nameColumn, atypeColumn, ageColumn, breedColumn, descriptionColumn, catColumn);
        return table;
    }

I want to sort the table by gender initially.

As far as I know I have to create a SortedList/FilteredList but the tutorial I found online is just confusing me even more.

Do I have to use SortedList/FilteredList or are there better alternatives?

Here's one of the links I found http://code.makery.ch/blog/javafx-8-tableview-sorting-filtering/

Can anyone dumb it down for me please?

3 Answers

I used the accepted answer to make it work but I want to share a complete solution.

In this case, I provided data to table before sorting. Set sort type for column that you want to sort, add that column to sort order, then call sort() method where ever you want to sort table.

myColumn.setSortType(TableColumn.SortType.DESCENDING);
myTableView.getSortOrder().add(myColumn);
myTableView.sort();

This statement already sorts.

tableView.getSortOrder().add(myColumn);

A subsequent call: tableView.sort() isn't necessary.

Related