Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8096819

Cannot sort bound TableView

    XMLWordPrintable

Details

    Description

      When a TableView's itemsProperty is bound to a data source, sorting will not work:
      - Clicking a column header causes the TableView to re-sort.
      - Sorting changes the collection that the tableView is bound to (I am not sure if this is okay or not, at least it happens).
      - This triggers the invalidated()-Method of the items-Property, which in turn clears out the sortOrder.
      - So clicking a column header to change the sort order does not have any effect.

      This issue does not happen if:
      - The TableView is not bound to an external property, but filled directly.
      - Or if the collection it is bound to is empty.

      This used to be different in earlier Versions of JavaFX 8. I think this issue is caused by the fix for https://javafx-jira.kenai.com/browse/RT-35763
      Another cause for this issue might be that sorting a TableView that is bound (not bidirectionally!) changes the collection that it is bound to. I don't know what the intended behaviour is in that case.

      I consider this critical - it should be possible to sort bound TableViews.


      Application to reproduce:

      import javafx.application.Application;
      import javafx.beans.property.SimpleListProperty;
      import javafx.beans.property.SimpleStringProperty;
      import javafx.collections.FXCollections;
      import javafx.scene.Scene;
      import javafx.scene.control.TableColumn;
      import javafx.scene.control.TableView;
      import javafx.stage.Stage;

      public class SortBoundTableView extends Application {

          public static void main(String[] args) {
              launch(args);
          }

          @Override
          public void start(Stage primaryStage) throws Exception {
              TableView<String> tableView = new TableView<String>();
              
              TableColumn<String, String> tableColumn = new TableColumn<String, String>();
              tableColumn.setCellValueFactory((data) -> { return new SimpleStringProperty(data.getValue()); });
              tableColumn.setText("Test");
              tableView.getColumns().add(tableColumn);
              
              SimpleListProperty<String> data = new SimpleListProperty<>(FXCollections.observableArrayList());
              tableView.itemsProperty().bind(data);
              
              // Note: Commenting this line out will make it "work".
              data.addAll("Element 1", "Element 2");
              
              primaryStage.setScene(new Scene(tableView));
              primaryStage.show();
          }

      }


      Probably Off-Topic: When using .bindBidirectionally() instead of .bind(), something (else?) goes terribly wrong and I get an exception - actually two different exceptions depending on whether the TableView is empty or not. However, this is not my primary concern.

      Attachments

        Activity

          People

            jgiles Jonathan Giles
            pkeckjfx Philipp Keck (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported: