【问题标题】:Items in ComboBox disappearComboBox 中的项目消失
【发布时间】: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


【解决方案1】:

消失的行为可能是一个错误。您可以将其提交给 JavaFX Jira,让 Oracle 人员进一步决定。此外,您可以调查 ComboBox.setCellFactory(...) 源代码以了解此行为的原因并找到解决方法。但我的建议是使用 ComboBox Cell 的 (ListCell) 内部 Labelled 组件,而不是您的:

@Override
public void updateItem(Label item, boolean empty) {
    super.updateItem(item, empty);
    if (item != null && !empty) {
        setText(item.getText());
        setGraphic(item.getGraphic());
    } else {
        setText(null);
        setGraphic(null);
    }
}

注意代码的else部分,在编写if语句时涵盖所有用例。

【讨论】:

  • 我会试试的。谢谢!
猜你喜欢
  • 1970-01-01
  • 2016-01-17
  • 2013-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多