java - Vaadin combobox with different properties to display and bind -


i have 2 domain classes

public class {   private string nick;   private string bid;    // getters & setters }  public class b {   private string id;   private string name;    // lot of other fields   // getter , setters } 

the idea a not save complete b, id.

now create form in vaadin (7.6.7) create new a. there limited number of b objects available, have combobox, user can select b.

as id of b non-user-friendly field, have combobox, bound property bid in a object , presents property name of b.

i cannot figure out how code shall like.

formlayout layout = new formlayout(); beanfieldgroup<a> databinder = new beanfieldgroup(a.class); field<?> nickfield = databinder.buildandbind("nick"); layout.addcomponent(nickfield);  combobox bbox = new combobox("b"); list<b> allbs = ... // bs; allbs.stream().foreach(bbox::additem); databinder.bind(bbox, "bid"); // not work 

i know problem have bound combobox type b field of type string, how make it, can show bs name in combobox, when commit happening, uses id of b ?

i recommend set item captions explicitly using combobox.setitemcaption(..):

for (final b b : allbs) {     bbox.setitemcaption(b.getid(), b.getname()); } 

then bind combobox' property a's bid , combobox' container data source list of b ids set yourself.

bbox.setcontainerdatasource(new beanitemcontainer<>(             string.class, allbids)); 

or manually set combobox items:

for (final b b : allbs) {     bbox.additem(b.getid()); } 

here example comboboxes might too.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -