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
相关产品推荐
相关产品推荐

