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

TableColumn: default comparator should use Collator for String types

    Details

      Description

      If the column type is String, the default comparator uses the type's comparable implementation which in turn doesn't cope with locale-dependent sort order.

      To reproduce, run the example below:

      - the rows are ordered "Abe, Be, abe, be"
      - expected (for German): "abe, Abe, be, Be"

      To fix, special case the String type and and don't use its comparabel but instead use the Collator for comparing (that's what Swing does)

      /**
       * Issue TableColumn's default comparator doesn't use collator for String types.
       *
       * @author Jeanette Winzenburg, Berlin
       */
      public class TableSortExample extends Application {

          private Parent getContent() {
              ObservableList<String> items = FXCollections.observableArrayList("Abe", "abe", "Be", "be");
              TableView<String> view = new TableView<String>(items);
              TableColumn<String, String> column = new TableColumn("Items");
              column.setPrefWidth(100);
              column.setCellValueFactory(data -> new SimpleStringProperty(data.getValue()));
              view.getColumns().addAll(column);
              view.getSortOrder().add(column);
              Pane parent = new HBox(100);
              parent.getChildren().addAll(view);
              parent.setPadding(new Insets(20));
              return parent;
          }
          
          @Override
          public void start(Stage primaryStage) throws Exception {
              Scene scene = new Scene(getContent());
              primaryStage.setScene(scene);
              primaryStage.show();
          }

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

        Attachments

          Activity

            People

            • Assignee:
              dgrieve David Grieve (Inactive)
              Reporter:
              fastegal Jeanette Winzenburg
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported: