【发布时间】:2013-09-18 17:09:21
【问题描述】:
我在使用 JavaFX 中的 Scene Builder 创建的 FXML 文件创建 ComboBox 的自定义单元工厂时遇到以下问题: 我创建了一个自定义的标签单元工厂。当用户单击项目时,它工作正常。 y 显示在“按钮”区域中。但是当用户想要点击另一个项目时,之前点击的项目就消失了。
这是组合框单元工厂的代码:
idCardOnlineStatusComboBox.setCellFactory(new Callback<ListView<Label>, ListCell<Label>>() {
@Override public ListCell<Label> call(ListView<Label> param) {
final ListCell<Label> cell = new ListCell<Label>() {
@Override public void updateItem(Label item,
boolean empty) {
super.updateItem(item, empty);
if(item != null || !empty) {
setGraphic(item);
}
}
};
return cell;
}
});
我想是电池工厂有问题,但我不知道是哪里。
我使用以下代码从 fxml 中提取组合框:
@FXML private ComboBox idCardOnlineStatusComboBox;
然后我用这个填充组合框:
idCardOnlineStatusComboBox.getItems().addAll(
new Label(Resource.getStringFor("MainForm.Pane.MenuBar.Vortex.OnlineStatus.Online.Title"), new ImageView(onlineImg)),
new Label(Resource.getStringFor("MainForm.Pane.MenuBar.Vortex.OnlineStatus.Away.Title"), new ImageView(awayImg)),
new Label(Resource.getStringFor("MainForm.Pane.MenuBar.Vortex.OnlineStatus.DoNotDisturb.Title"), new ImageView(doNotDisturbImg)),
new Label(Resource.getStringFor("MainForm.Pane.MenuBar.Vortex.OnlineStatus.Invisible.Title"), new ImageView(offlineImg)),
new Label(Resource.getStringFor("MainForm.Pane.MenuBar.Vortex.OnlineStatus.Offline.Title"), new ImageView(offlineImg))
);
【问题讨论】:
-
你的代码都在这里吗?请发布SSCCE,我们可以尝试查看错误。
标签: java combobox javafx-2 javafx fxml