You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vaadin中如何预选中ComboBox的选项?

Vaadin ComboBox 预选中选项的替代方式

Vaadin 的 ComboBox 没有 select() 方法,要实现预选中,直接调用 setValue() 方法传入目标选项即可。

关键注意点

  • 如果是枚举类,直接传入目标枚举值就行——枚举默认的 equals() 和 hashCode() 基于实例实现,能被 Vaadin 正确识别匹配;
  • 如果是自定义对象,必须正确重写 equals() 和 hashCode(),否则 Vaadin 无法判定选项是否匹配。

修改后的示例代码

final ComboBox<ReportTypeEnum> selectionBox = new ComboBox<>("Rapporttyp");
selectionBox.setWidthFull();
List<ReportTypeEnum> reportTypes = getPatentNationalReportTypeList();
selectionBox.setItems(reportTypes);

// 预选中列表第一个选项(需先判断列表非空)
if (!reportTypes.isEmpty()) {
    selectionBox.setValue(reportTypes.get(0));
}

// 也可以直接指定某个枚举值,比如:
// selectionBox.setValue(ReportTypeEnum.TARGET_TYPE);

selectionBox.addValueChangeListener(e -> setVisibilityOfGenerateReportTypeButton());

内容的提问来源于stack exchange,提问作者Eyvind Almqvist

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 06:13:11