user3461443
user3461443

Reputation: 231

How do I disable column sorting in JavaFX TableView

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?

Upvotes: 12

Views: 13744

Answers (3)

Wiitry
Wiitry

Reputation: 19

/**
 * 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;
}

Upvotes: -1

yimkong
yimkong

Reputation: 71

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

<TableColumn sortable="false"/>

Upvotes: 3

aw-think
aw-think

Reputation: 4803

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);

Upvotes: 33

Related Questions