JavaFX TableView 点击排序器不工作?
JavaFX TableView click sorters not working?
为什么我的 TableColumn.setSortable() 在 table header 上显示排序图形,而我在 double-click 上显示排序图形,但它实际上并没有在全部?我想它自然知道如何对数字进行排序?我是否必须为具有自然排序行为的类型设置显式比较器?
public class PenaltyDashboardManager {
private final TableView<Penalty> penaltyTable = new TableView<Penalty>();
/* ... */
private void initializeTable() {
penaltyTable.setItems(Penalty.getPenaltyManager().getPenalties());
penaltyTable.setEditable(true);
TableColumn<Penalty,Number> penaltyId = new TableColumn<>("ID");
penaltyId.setCellValueFactory(c -> c.getValue().getPenaltyIdProperty());
penaltyId.setEditable(true);
penaltyId.setSortable(true);
/* ... */
penaltyTable.getColumns.add(penaltyId);
}
}
更新
很奇怪。我试图创建一个简单的示例来演示排序不起作用。但是这个简单的整数列排序得很好:/
public final class TableSortTest extends Application {
private static final ObservableList<NumericCombo> values = FXCollections.observableList(
IntStream.range(1, 100).mapToObj(i -> new NumericCombo()).collect(Collectors.toList()));
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Collections.shuffle(values);
TableView<NumericCombo> tableView = new TableView<>();
tableView.setItems(values);
TableColumn<NumericCombo,Number> combo1 = new TableColumn<>("COMBO 1");
combo1.setCellValueFactory(c -> new ReadOnlyObjectWrapper<>(c.getValue().combo1));
TableColumn<NumericCombo,Number> combo2 = new TableColumn<>("COMBO 2");
combo2.setCellValueFactory(c -> c.getValue().combo2);
TableColumn<NumericCombo,Number> combo3 = new TableColumn<>("COMBO 3");
combo3.setCellValueFactory(c -> c.getValue().combo3);
tableView.getColumns().addAll(combo1,combo2,combo3);
Group root = new Group(tableView);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
private static final class NumericCombo {
private static final Random rand = new Random();
private final int combo1;
private final IntegerProperty combo2;
private final IntegerProperty combo3;
private NumericCombo() {
combo1 = rand.nextInt((10000 - 0) + 1);
combo2 = new SimpleIntegerProperty(rand.nextInt((10000 - 0) + 1));
combo3 = new SimpleIntegerProperty(rand.nextInt((10000 - 0) + 1));
}
}
}
我找到问题了!我正在使用我自己的 ObservableList 实现,称为 ObservableImmutableList。它将 ObservableList 接口包装在 Guava ImmutableList 周围。由于 ImmutableList 不可修改,因此无法对其进行排序......即使在 TableView 中也是如此。
这转移到另一个我正在努力解决的问题。如何对 ObservableImmutableList
进行排序? .
为什么我的 TableColumn.setSortable() 在 table header 上显示排序图形,而我在 double-click 上显示排序图形,但它实际上并没有在全部?我想它自然知道如何对数字进行排序?我是否必须为具有自然排序行为的类型设置显式比较器?
public class PenaltyDashboardManager {
private final TableView<Penalty> penaltyTable = new TableView<Penalty>();
/* ... */
private void initializeTable() {
penaltyTable.setItems(Penalty.getPenaltyManager().getPenalties());
penaltyTable.setEditable(true);
TableColumn<Penalty,Number> penaltyId = new TableColumn<>("ID");
penaltyId.setCellValueFactory(c -> c.getValue().getPenaltyIdProperty());
penaltyId.setEditable(true);
penaltyId.setSortable(true);
/* ... */
penaltyTable.getColumns.add(penaltyId);
}
}
更新
很奇怪。我试图创建一个简单的示例来演示排序不起作用。但是这个简单的整数列排序得很好:/
public final class TableSortTest extends Application {
private static final ObservableList<NumericCombo> values = FXCollections.observableList(
IntStream.range(1, 100).mapToObj(i -> new NumericCombo()).collect(Collectors.toList()));
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Collections.shuffle(values);
TableView<NumericCombo> tableView = new TableView<>();
tableView.setItems(values);
TableColumn<NumericCombo,Number> combo1 = new TableColumn<>("COMBO 1");
combo1.setCellValueFactory(c -> new ReadOnlyObjectWrapper<>(c.getValue().combo1));
TableColumn<NumericCombo,Number> combo2 = new TableColumn<>("COMBO 2");
combo2.setCellValueFactory(c -> c.getValue().combo2);
TableColumn<NumericCombo,Number> combo3 = new TableColumn<>("COMBO 3");
combo3.setCellValueFactory(c -> c.getValue().combo3);
tableView.getColumns().addAll(combo1,combo2,combo3);
Group root = new Group(tableView);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
private static final class NumericCombo {
private static final Random rand = new Random();
private final int combo1;
private final IntegerProperty combo2;
private final IntegerProperty combo3;
private NumericCombo() {
combo1 = rand.nextInt((10000 - 0) + 1);
combo2 = new SimpleIntegerProperty(rand.nextInt((10000 - 0) + 1));
combo3 = new SimpleIntegerProperty(rand.nextInt((10000 - 0) + 1));
}
}
}
我找到问题了!我正在使用我自己的 ObservableList 实现,称为 ObservableImmutableList。它将 ObservableList 接口包装在 Guava ImmutableList 周围。由于 ImmutableList 不可修改,因此无法对其进行排序......即使在 TableView 中也是如此。
这转移到另一个我正在努力解决的问题。如何对 ObservableImmutableList
进行排序?