-
Bug
-
Resolution: Fixed
-
P4
-
8u5
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();
}
}
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();
}
}