我正在使用 Vaadin 6,需要一些帮助来设置用户从下拉框中选择一个选项后所选择的值。创建和添加一些值到选择框很容易:
Select sel = new Select();
sel.addItem("Value 1");
sel.addItem("Value 2");
sel.addItem("Value 3");
获取值同样简单:
String selValue = (String) sel.getValue();
但是问题出在我需要填充 Select 的 HashMap 的地方:
HashMap<String, String> users = new HashMap<String, Sting>();
users.put("1", "John");
users.put("2", "Bob");
users.put("3", "Tom");
所以在选择框中我想显示实际的用户名,但是当它被选中时我想传递id。这可能吗?还是所见即所得?
最佳答案
你需要做的:
HashMap<String, String> users = new HashMap<String, String>();
users.put("1", "John");
users.put("2", "Bob");
users.put("3", "Tom");
Select select = new Select();
for(Iterator<String> i = users.keySet().iterator(); i.hasNext();) {
String key = i.next();
select.addItem(key);
select.setItemCaption(key, users.get(key));
}
String selValue = (String) select.getValue();
这样就可以像您一样添加项目,但随后会设置用户将在屏幕上看到的内容。然后当你获取值时,它会返回id,而不是显示的值。
关于java - 如何设置Vaadin 6 'Select'框选择值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25758597/