Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
New ComboBox Help
Help please.
My models are:
Usuario:
...
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String nombre;
private String apepat;
private String apemat;
private String fono;
private String celular;
private String estado;
@ManyToOne
@JoinColumn(name="ciudad_id")
private Ciudad ciudad;
public Ciudad getCiudad() {
return ciudad;
}
public void setCiudad(Ciudad ciudad) {
this.ciudad = ciudad;
}
.....
Ciudad:
....
@OneToMany(mappedBy="ciudad")
private List<Usuario> usuario=new ArrayList<Usuario>();
public List<Usuario> getUsuario() {
return usuario;
}
public void setUsuario(List<Usuario> usuario) {
this.usuario = usuario;
}
....
I need to get the element is selected of a table in a ComboBox, my code is the next:
CiudadDAO cDAO=new CiudadDAO();
BeanItemContainer bic = new BeanItemContainer<Ciudad>(Ciudad.class, cDAO.mostrarTodo());
comboCiudad.setItemCaptionPropertyId("nombre");
comboCiudad.setContainerDataSource(bic);
UsuarioDAO uDAO = new UsuarioDAO();
List<Usuario> c = uDAO.buscarPorId(id);
for (int i = 0; i < uDAO.buscarPorId(id).size(); i++) {
System.out.println("" + i);
System.out.println("NOMBRE" + c.get(i).getNombre());
txtNombre.setValue(c.get(i).getNombre());
txtApepat.setValue(c.get(i).getApepat());
txtApemat.setValue(c.get(i).getApemat());
txtFono.setValue(c.get(i).getFono());
txtCelular.setValue(c.get(i).getCelular());
comboCiudad.setNewItemsAllowed(true);
[b] comboCiudad.setValue( );[/b]
txtEstado.setValue(c.get(i).getEstado());
}
Hi!
If I understood your example and problem correctly, and the 'ciudad' doesn't exist in the comboCiudad selections yet, you first need to call something like comboCiudad.addItem(c.get(i).getCiudad()); and only then you can select it using the comboCiudad.setValue(c.get(i).getCiudad()); In fact, you shouldn't need the setNewItemsAllowed(true); at all, unless you specifically want that your users can add new items to the ComboBox on the fly. Programmatic adding is always allowed unless the ComboBox is readOnly.
Yep, i solved this:
comboCiudad.setNewItemsAllowed(true);
comboCiudad.addItem(c.get(i).getCiudad().getNombre());
comboCiudad.setValue(c.get(i).getCiudad().getNombre());
Thanks.