Button 如何通过单击按钮检索新添加到vaadin组合框的项
我的问题很简单。我试图通过单击一个按钮将一个新项目添加到已经填充了一些数据的Vaadin组合框中。我希望新添加的项在按钮单击事件处理程序中可用,以便将其添加到数据库表中Button 如何通过单击按钮检索新添加到vaadin组合框的项,button,combobox,vaadin,Button,Combobox,Vaadin,我的问题很简单。我试图通过单击一个按钮将一个新项目添加到已经填充了一些数据的Vaadin组合框中。我希望新添加的项在按钮单击事件处理程序中可用,以便将其添加到数据库表中 ComboBox region = new ComboBox(); for (RegionDetails details : regions) { int regionId = details.getRegionId(); String regionName = details.getRegionName();
ComboBox region = new ComboBox();
for (RegionDetails details : regions) {
int regionId = details.getRegionId();
String regionName = details.getRegionName();
region.addItem(regionId);
region.setItemCaption(regionId, regionName);
}
Button addR = new Button("Add");
addR.addClickListener(new ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
// how do I reference the item here?!
}
});
我试了好几次,但没有找到线索
有人请帮帮我。提前感谢。如果您还没有这样做,您应该看看瓦丁手册/文档中的章节。它使您的生活更轻松,因为Vaadin将自动完成大部分工作,因此您不必手动定义标题、字段等。可能有一些情况不适合这样做,但这些情况很少,不在问题的讨论范围内 您可以在下面看到一个基于先前代码和一些虚拟数据的示例。我所做的是使用BeanItemContainer收集组合的项目数据,并使用其name属性使Vaadin生成标题,而不必手动设置标题。此外,在click listener中,您只需将bean添加到容器中,然后将其保存到DB或您需要执行的任何操作:
public class MyComboBoxComponent extends VerticalLayout {
// bean property to use for caption generation
private static final String CAPTION_PROPERTY = "name";
public MyComboWithItemIDComponent() {
// basic combo instantiation
ComboBox comboBox = new ComboBox();
// use a bean item container
BeanItemContainer<RegionDetails> itemContainer = new BeanItemContainer<>(RegionDetails.class);
comboBox.setContainerDataSource(itemContainer);
// configure the combo to use the "name" property of each item as their caption
// so we don't need to manually set it... magic
comboBox.setItemCaptionMode(AbstractSelect.ItemCaptionMode.PROPERTY);
comboBox.setItemCaptionPropertyId(CAPTION_PROPERTY);
// add some dummy data
itemContainer.addAll(generateSomeDummyDetails());
// add button
Button addButton = new Button("Add", event -> {
// create a new bean instance and populate it accordingly
RegionDetails newRegionDetails = new RegionDetails(itemContainer.size(), "Region - " + itemContainer.size());
// add it to the container
itemContainer.addItem(newRegionDetails);
// do whatever you want with the newRegionDetails here
});
// add the components to the layout
addComponent(comboBox);
addComponent(addButton);
}
// method to generate some dummy data
private List<RegionDetails> generateSomeDummyDetails() {
List<RegionDetails> dummyDetails = new ArrayList<>();
for (int i = 0; i < 10; i++) {
dummyDetails.add(new RegionDetails(i, "Region - " + i));
}
return dummyDetails;
}
// the model bean
public class RegionDetails {
private final int id;
private final String name;
public RegionDetails(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
}
公共类MyComboxComponent扩展了垂直布局{
//用于标题生成的bean属性
私有静态最终字符串CAPTION_PROPERTY=“name”;
公共MyComboWithItemIDComponent(){
//基本组合实例化
ComboBox ComboBox=新建ComboBox();
//使用bean项容器
BeanItemContainer itemContainer=新的BeanItemContainer(RegionDetails.class);
comboBox.setContainerDataSource(itemContainer);
//将组合配置为使用每个项目的“name”属性作为其标题
//所以我们不需要手动设置它…神奇
comboBox.setItemCaptionMode(AbstractSelect.ItemCaptionMode.PROPERTY);
comboBox.setItemCaptionPropertyId(CAPTION_属性);
//添加一些虚拟数据
addAll(generatesomedummydeails());
//添加按钮
按钮添加按钮=新按钮(“添加”,事件->{
//创建一个新的bean实例并相应地填充它
RegionDetails newRegionDetails=newRegionDetails(itemContainer.size(),“Region-”+itemContainer.size());
//将其添加到容器中
itemContainer.addItem(newRegionDetails);
//你想用这里的newRegionDetails做什么都行
});
//将组件添加到布局中
添加组件(组合框);
添加组件(添加按钮);
}
//方法生成一些虚拟数据
私有列表generateSomeDummyDetails(){
List dummyDetails=新建ArrayList();
对于(int i=0;i<10;i++){
添加(新区域详细信息(i,“区域-”+i));
}
返回详细信息;
}
//模型bean
公共类区域详细信息{
私有最终int id;
私有最终字符串名;
公共区域详细信息(int-id,字符串名称){
this.id=id;
this.name=名称;
}
公共int getId(){
返回id;
}
公共字符串getName(){
返回名称;
}
}
}
结果是:
如果您还没有这样做,您应该看看瓦丁手册/文档中的章节。它使您的生活更轻松,因为Vaadin将自动完成大部分工作,因此您不必手动定义标题、字段等。可能有一些情况不适合这样做,但这些情况很少,不在问题的讨论范围内 您可以在下面看到一个基于先前代码和一些虚拟数据的示例。我所做的是使用BeanItemContainer收集组合的项目数据,并使用其name属性使Vaadin生成标题,而不必手动设置标题。此外,在click listener中,您只需将bean添加到容器中,然后将其保存到DB或您需要执行的任何操作:
public class MyComboBoxComponent extends VerticalLayout {
// bean property to use for caption generation
private static final String CAPTION_PROPERTY = "name";
public MyComboWithItemIDComponent() {
// basic combo instantiation
ComboBox comboBox = new ComboBox();
// use a bean item container
BeanItemContainer<RegionDetails> itemContainer = new BeanItemContainer<>(RegionDetails.class);
comboBox.setContainerDataSource(itemContainer);
// configure the combo to use the "name" property of each item as their caption
// so we don't need to manually set it... magic
comboBox.setItemCaptionMode(AbstractSelect.ItemCaptionMode.PROPERTY);
comboBox.setItemCaptionPropertyId(CAPTION_PROPERTY);
// add some dummy data
itemContainer.addAll(generateSomeDummyDetails());
// add button
Button addButton = new Button("Add", event -> {
// create a new bean instance and populate it accordingly
RegionDetails newRegionDetails = new RegionDetails(itemContainer.size(), "Region - " + itemContainer.size());
// add it to the container
itemContainer.addItem(newRegionDetails);
// do whatever you want with the newRegionDetails here
});
// add the components to the layout
addComponent(comboBox);
addComponent(addButton);
}
// method to generate some dummy data
private List<RegionDetails> generateSomeDummyDetails() {
List<RegionDetails> dummyDetails = new ArrayList<>();
for (int i = 0; i < 10; i++) {
dummyDetails.add(new RegionDetails(i, "Region - " + i));
}
return dummyDetails;
}
// the model bean
public class RegionDetails {
private final int id;
private final String name;
public RegionDetails(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
}
公共类MyComboxComponent扩展了垂直布局{
//用于标题生成的bean属性
私有静态最终字符串CAPTION_PROPERTY=“name”;
公共MyComboWithItemIDComponent(){
//基本组合实例化
ComboBox ComboBox=新建ComboBox();
//使用bean项容器
BeanItemContainer itemContainer=新的BeanItemContainer(RegionDetails.class);
comboBox.setContainerDataSource(itemContainer);
//将组合配置为使用每个项目的“name”属性作为其标题
//所以我们不需要手动设置它…神奇
comboBox.setItemCaptionMode(AbstractSelect.ItemCaptionMode.PROPERTY);
comboBox.setItemCaptionPropertyId(CAPTION_属性);
//添加一些虚拟数据
addAll(generatesomedummydeails());
//添加按钮
按钮添加按钮=新按钮(“添加”,事件->{
//创建一个新的bean实例并相应地填充它
RegionDetails newRegionDetails=newRegionDetails(itemContainer.size(),“Region-”+itemContainer.size());
//将其添加到容器中
itemContainer.addItem(newRegionDetails);
//你想用这里的newRegionDetails做什么都行
});
//将组件添加到布局中
添加组件(组合框);
添加组件(添加按钮);
}
//方法生成一些虚拟数据
私有列表generateSomeDummyDetails(){
List dummyDetails=新建ArrayList();
对于(int i=0;i<10;i++){
添加(新区域详细信息(i,“区域-”+i));
}
返回详细信息;
}
//模型bean
公共类区域详细信息{
私有最终int id;
私有最终字符串名;
公共区域详细信息(int-id,字符串名称){
this.id=id;
这