I am trying to bind an ArrayList of objects to a Java SWT List widget. This is what I have:
DataBindingContext bindingContext = new DataBindingContext();
//
myModel= new WritableList(buses, MyObject.class);
IObservableList listWidgetObs = SWTObservables.observeItems(listWidget);
bindingContext.bindList(listWidgetObs , myModel, null, null);
//
return bindingContext;
But somehow, it doesn’t seem to work. I have been trying for a long time but still with no luck. This is my first time trying data binding in Java. How do I bind an ArrayList as the data provider of an SWT List widget and then bind it to the value of a method call getName() in MyObject class?
Thanks!
From your example, it looks like the model list (
myModel) contains objects of typeMyObject. But this is wrong as the content list of an SWTListmust by be Strings.You have (at least) three choices:
Stringso you must convert the objects as you create the observable list, orUpdateListStrategyinbindingList(...), orListViewerwith aLabelProviderThe choice depends on whether the objects of
myModelcan change dynamically.I usually choose the second option when possible as I try to not mix both databinding and JFace unless it is really, really needed. The alternative can sometimes get some rather ugly notification/listener chains…