How do I disable column sorting in JavaFX TableView
Asked Answered
I

3

12

I have noticed that sorting by column headers in the JavaFX TableView class is turned on by default.

In my case I need to have a table view that does not allow sorting by any of the columns. Does anyone know how to do this?

Indopacific answered 3/7, 2015 at 21:10 Comment(0)
K
33

If you created the table columns at own instances like this:

TableColumn<Type, Type> column = new TableColumn<>("email");

then you can easily set

column.setSortable(false);
Kuntz answered 4/7, 2015 at 6:9 Comment(0)
S
3

if you use fxml, you can also set the column unsortable in fxml:

<TableColumn sortable="false"/>
Suet answered 13/7, 2021 at 8:32 Comment(0)
V
-1
/**
 * Prevent a TableView from doing TableColumn sorting.
 *
 * @param _tableView
 * @param _actionPrevent  :set ON and OFF the 'Sortability' of columns.
 */
static
public < T > void preventColumnSorting( TableView< T > _tableView, boolean _actionPrevent )
{
    Platform.runLater( () ->
    {
        _tableView.getColumns()
            .forEach( column_ ->
            {
                column_.setSortable( ! _actionPrevent );
            } );
    } );

    return;
}
Vermicelli answered 25/10, 2021 at 12:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.