问题
My problem is a simple one. I am trying to add a new Item to a Vaadin ComboBox, which is already populated with some data, by clicking a button. I want the newly added item to be available in the button click event handler so that I can add the same to database table.
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?!
}
});
I tried multiple times, but not getting the clue.
Someone please help me. Thanks in advance.
回答1:
If you haven't already done so, you should take a look at the collecting items in containers chapter from the Vaadin book/docs. It makes your life easier as Vaadin will do most of the work automatically, so you don't have to manually define captions, fields, etc. There may be situations where this is not suitable but those are few and it's not in the question's scope to discuss them.
You can see below a sample based on your previous code and some dummy data. What I did was to use a BeanItemContainer to collect the item data for the combo, and use their name property to make Vaadin generate a caption instead of having to manually set it. Furthermore, in the click-listener, you simply add the bean to the container, and afterwards save it to the DB or whatever you need to do:
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;
}
}
}
The result:
来源:https://stackoverflow.com/questions/39445423/how-to-retrieve-the-newly-added-item-to-vaadin-combobox-with-a-button-click