JDK-8117875 : Sorting a table column leaves the selection model inconsistent with the display in the UI
  • Type: Bug
  • Component: javafx
  • Sub-Component: controls
  • Affected Version: 7u15,8
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • Submitted: 2013-05-01
  • Updated: 2015-06-17
  • Resolved: 2013-07-31
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8
8Fixed
Related Reports
Relates :  
Description
If a table row is selected and then the table is sorted (by clicking on a column header), the selection model is updated to indicate that the selection is cleared. However, the display still shows the same item as being selected.

To reproduce, run the following sample. 
Select one row in the table. 
Click on the column header to sort by that row. Note that the item remains highlighted, indicating it is selected.
Press the button to dump the selection model data. Note that the selectedIndex is -1 and the selectedItem is null.

OTN forum discussion here: https://forums.oracle.com/forums/thread.jspa?forumID=1385&threadID=2531317
Comments
I have a problem when I use a combobox linked to list the items on the table view. Something like adding the following code: ComboBox cb = new ComboBox(); cb.setItems(table.getItems()); root.setTop(cb); When I sort the column the combo box change too but the natural behavior should be to keep the value. It's that fixed too? If I'm obligated to use the SDK 1.7.0_45, there is any walkaround for this?
11-11-2013

Verified on 8.0b114
06-11-2013

Fixed for TableView and TreeTableView.
31-07-2013

I've developed a unit test and a fix, now to just fix the other tests that failed....
31-07-2013

The basic summary is that the selectedIndex and selectedItem properties are reset when the column is sorted, but the selectedIndices and selectedItems lists are not reset (and in fact they are updated to represent the new item locations). This explains why the selection visually follows as expected, but that the output from the printlns in the code above is not correct. If you update the code to also print out selectedIndices and selectedItems as well you will note that they are correct before and after sorting.
31-07-2013

import javafx.application.Application; import javafx.beans.property.IntegerProperty; import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.SelectionModel; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; public class TableSortSelectionTest extends Application { @Override public void start(Stage primaryStage) { final TableView<IntegerProperty> table = new TableView<>(); table.getItems().addAll(new SimpleIntegerProperty(1), new SimpleIntegerProperty(2), new SimpleIntegerProperty(3)); final TableColumn<IntegerProperty, Integer> col = new TableColumn<>("Value"); col.setCellValueFactory(new PropertyValueFactory<IntegerProperty, Integer>("value")); table.getColumns().add(col); table.getSelectionModel().selectedItemProperty() .addListener(new ChangeListener<IntegerProperty>() { @Override public void changed(ObservableValue<? extends IntegerProperty> observable, IntegerProperty oldValue, IntegerProperty newValue) { if (newValue == null) { System.out.println("Nothing selected"); } else { System.out.println(newValue.getValue()); } } }); Button button = new Button("Show selection model data"); button.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { SelectionModel<IntegerProperty> selectionModel = table .getSelectionModel(); System.out.println("Selected index: " + selectionModel.getSelectedIndex()); System.out.println("Selected item: " + selectionModel.getSelectedItem()); } }); final BorderPane root = new BorderPane(); root.setCenter(table); root.setBottom(button); primaryStage.setScene(new Scene(root, 200, 400)); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
01-05-2013