0

guys,

my Program has a lot of TableViews. In all tables except one sorting is working. I can't find my mistake in that single TableViews. When clicking the header it seems like its sorting but not refreshing the view. If I change the tab and go back, the TableView is sorted. If a row is selected and I click on the header the selected color is sorted on the right spot but not the table data.

I have my ObservableList and TableView:

private ObservableList<ProjectViewModel>    projectViewModelList = null;

// some more TableColumns
@FXML
TableColumn<ProjectViewModel, String>       TableColumnToolchainStepphase;
@FXML
TableView<ProjectViewModel>                 TableViewToolchain;

my initialize:

@FXML
public void initialize(){
    //...
    initializeLists();
    //...
    initTableViewToolchain();
    initTableColumns();
    //...
}

private void initializeLists(){
    projectViewModelList = FXCollections.observableArrayList();
}
private void initTableViewToolchain(){
    TableViewToolchain.setItems( projectViewModelList );
}
private void initTableColumns(){
    TableColumnToolchainStepphase
            .setCellValueFactory( new Callback<CellDataFeatures<ProjectViewModel, String>, ObservableValue<String>>() {

                @Override
                public ObservableValue<String> call( CellDataFeatures<ProjectViewModel, String> param ){
                    String string = param.getValue().getStepphaseString();
                    return new ReadOnlyStringWrapper( string );
                }
            } );

}

my table population:

private void setProjectViewModelList( Collection<ProjectViewModel> projectViewModelCollection ){
    projectViewModelList.clear();
    projectViewModelList.setAll( projectViewModelCollection );
}

Where did i go wrong?

Moone
  • 38
  • 5
  • Maybe this SO question (and answer) will help. [Sort TableView by certain column Javafx](https://stackoverflow.com/questions/36240142/sort-tableview-by-certain-column-javafx) – Abra Mar 04 '20 at 07:23
  • It doesn't help. My TableView gets pre-sorted. But when clicking again on the header the sort arrow goes to descending but the table is still sorted in ascending order. The sort alg is working. But the TableView does not reload. Changing Tabs gives me a sorted table. – Moone Mar 04 '20 at 07:41
  • I have a workaround, but my question is still valid i would like to know the cause. `TableViewToolchain.setOnMouseClicked( ( MouseEvent event ) -> { TableViewToolchain.sort(); TableViewToolchain.refresh(); } );` – Moone Mar 04 '20 at 08:19
  • Looking at your code, I can't see what causes your `TableView` to behave in the way you describe. In order to help you, I would like to copy and run your code on my machine. In order for me to do that, I ask you to [edit] your question and post a [mcve]. – Abra Mar 04 '20 at 12:54

1 Answers1

0

I did a workaround i force the sort and refresh on mouseclick:

TableViewToolchain.setOnMouseClicked( ( MouseEvent event ) ->{
   TableViewToolchain.sort();
   TableViewToolchain.refresh();
} );

So when clicking on those coulmns the sort happens as intended.

Moone
  • 38
  • 5