【问题标题】:Load in a specific name in combobox as first row在组合框中加载特定名称作为第一行
【发布时间】:2018-11-09 07:02:05
【问题描述】:

我有一个带有 id 和名称的模型。 I put the model object into a combobox. From the model i take the name atribute and make that the visual part, the only problem is. When you start up the 将组合框编程为空。 您必须单击它并选择第二行才能看到该项目。我想立即查看该项目。这可能吗?

public class ItemCell extends ListCell<Model> {
    @Override
    public void updateItem(Model person, boolean empty) {
        super.updateItem(person, empty);
        setText(person == null ? "" : person.getFirstName());
    }
}

在我的视图类中,我有:

ComboBox<Model> comboBox = new ComboBox<>();
comboBox.setCellFactory(lv -> new ItemCell());
comboBox.setButtonCell(new ItemCell());
comboBox.valueProperty().addListener((o, oldValue, newValue) -> {
      personModelFromCombobox = otherObject.getPerson();
});

正如您所见,一切都是正确的,只是您必须单击它并选择第二行才能看到该项目。

【问题讨论】:

    标签: java javafx combobox


    【解决方案1】:

    发生这种情况是因为您没有在 comboBox 中选择项目

    comboBox.getSelectionModel().select(index);
    

    其中index 是要在选择模型中选择的项目的integer position,或者是来自arrayList 且类型相同的value

    【讨论】:

      猜你喜欢
      • 2016-09-01
      • 1970-01-01
      • 2017-12-13
      • 2014-08-11
      • 1970-01-01
      • 1970-01-01
      • 2015-07-28
      • 2021-07-01
      • 1970-01-01
      相关资源
      最近更新 更多