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 b开发者_开发知识库ind 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 type MyObject
. But this is wrong as the content list of an SWT List
must by be Strings.
You have (at least) three choices:
- the objects in the list must be of type
String
so you must convert the objects as you create the observable list, or - your must supply a
UpdateListStrategy
inbindingList(...)
, or - you bind with a
ListViewer
with aLabelProvider
The choice depends on whether the objects of myModel
can 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...
精彩评论