【发布时间】:2015-08-18 07:24:35
【问题描述】:
我有一个组合框,其中包含Dog 类型的项目。如果所有项目都替换为新项目(通过ObservableList 模型上的setAll),项目渲染器可以应对此更新,而按钮单元格渲染器则不能:
这是重现问题的最小示例 (full source incl. imports on GitHub):
public class ComboBoxRefresh extends Application {
private static final class Dog {
private final String name;
public Dog(String name) {
this.name = name;
}
}
private static final class DogListCell extends ListCell<Dog> {
@Override
public void updateItem(Dog item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setText("");
} else {
setText(item.name);
}
}
}
private static List<Dog> createThreeDogs() {
return range(0, 3).mapToObj(i -> new Dog("Buddy " + i)).collect(toList());
}
@Override
public void start(Stage stage) throws Exception {
ObservableList<Dog> items = observableArrayList(createThreeDogs());
ComboBox<Dog> comboBox = new ComboBox<>(items);
comboBox.setPrefWidth(400);
comboBox.setCellFactory(listView -> new DogListCell());
comboBox.setButtonCell(new DogListCell());
Button button = new Button("Refresh");
button.setOnAction(event -> {
List<Dog> newItems = createThreeDogs();
items.setAll(newItems);
});
VBox box = new VBox(10, comboBox, button);
box.setPadding(new Insets(10));
Scene scene = new Scene(box);
stage.setScene(scene);
stage.show();
}
}
如果我将 equals 实现添加到 Dog 类,一切正常,但在我的实际应用程序中这不是一个选项。
是否有任何变通方法来强制正确刷新按钮单元格?
【问题讨论】:
-
我看到了类似的东西。似乎当项目列表中不存在所选值时,所选值是使用 .toString 而不是按钮单元格呈现的。你提交错误报告了吗?