Rounak
Rounak

Reputation: 611

How to get a record (item) number column in javafx table view?

I have populated a JavaFX TableView with columns of a table from a MySQL database.

In my TableView, the first column I have created is "Number" which is not linked to any column in MySql database table. In this column, I want to see the number of row against each row.

For example, if there are five rows in the TableView, the "Number" column must show the numbers 1,2,3,4,5 respectively for each row. If row number 4 is deleted, the row numbers should be 1,2,3,4 for the remaining rows.

Upvotes: 1

Views: 2382

Answers (1)

Uluk Biy
Uluk Biy

Reputation: 49185

You need to define cellFactory of an extra TableColumn with no property name defined:

TableColumn numberCol = new TableColumn( "Number" );
numberCol.setCellFactory( new Callback<TableColumn, TableCell>()
{
    @Override
    public TableCell call( TableColumn p )
    {
        return new TableCell()
        {
            @Override
            public void updateItem( Object item, boolean empty )
            {
                super.updateItem( item, empty );
                setGraphic( null );
                setText( empty ? null : getIndex() + 1 + "" );
            }
        };
    }
});

Upvotes: 5

Related Questions