Binding data to Combobox

Hi,

I am trying to implement following steps.

  1. fetch data from database
  2. assign values to bean
  3. create form to list values in a combo box (there is problem with binding here)

Following code for each relevant steps:

  1. db resultset to set value in bean object :::

     	while(resultSet.next()) {					
     		JobsList obj = new JobsList();				
     		List<String> lst = new ArrayList<String>();
     		lst.add(startTime);
     		lst.add(endTime);
     						
     		obj.setWorkingHours(lst);
     						
     		jobs.add(obj);
     		lst.clear();
     	}
    
  2. Bean code:::

     private List<String> workingHours;
     public List<String> getWorkingHours() {
     	return workingHours;
     }
    
     public void setWorkingHours(List<String> workingHours) {
     	this.workingHours = workingHours;
     }
    
  3. Form code for binding:::

     private ComboBox<List<String>> workingHours = new ComboBox<List<String>>("Available Work Hours");
     private Binder<JobsList> binder = new Binder<>(JobsList.class);
    
     binder.forField(workingHours).bind(JobsList::getWorkingHours, JobsList::setWorkingHours);	
     add(title,workingHours,buttons);
    

Above bindings are automatically mapped with textfields but not with combo box. How do we bind combobox ui component to list values from a bean. Please help to resolve this issue.

You have defined a ComboBox with the generic type List<String>. This would mean that each entry in the ComboBox is a list of Strings. So you’d have a dropdown like

  "a", "b", "c"
  "d", "e"
  "f", "g", "h", "i"

(and that’s assuming a suitable ItemLabelGenerator or Renderer). I’m not sure if that’s what you’re after? If you want to pick one String off the list, then you should instead create a DataProvider for the ComboBox. ListDataProvider is probably a good choice for that.

-Olli

Hello Olli,

You are explaining a straight forward Combobox behavior. That is not what I am trying.

I have a java bean of list that shall be bind to combobox component. My question is more about data binding with controls. With a text filed data binding is straight forward and working fine. I want to achieve this binding with combobox.

Please refer my code above for more information.
Thank you.

I guess I don’t understand what you’re trying to do. What are you trying to bind to the combo box? Which value should be updated when a value is selected from the combo box?

Hi people!,
This is my question:
I have a simple combobox alpha with some fixed values example: “A”, “B”, “C”…ok?
then I have a value tango to do the binder on the combobox object, so
i do like this: binder.bind(alpha, “tango”);
result: if tango is equal to some combo values is all ok, in the form i see the combobox
with the right selection ready!
But if the tango value is not listed? I see a space and nothing selected…
ok, I can hunderstand why, but I need an error! this is the problem! I need to
manage a NON binded data! how can i do? thank you so much! :slight_smile: