Be very careful when using data binding with SWT control.

Be very careful when using data binding with SWT control.
If we observe a property of SWT controls, and bind it to another observable value, you must take care of those method calls which will change the property indirectly.
for example, we have a Combo whose "text" property is bound to a bean's "name" property like this:
IObservableValue nameObservable = BeansObservables.observeDetailValue(obserabedDriverProfile, "name", String. class);

ISWTObservableValue nameComboObservable = SWTObservables.observeText(driverClassCombo);

bindingContext.bindValue(nameComboObservable , nameObservable,  new UpdateValueStrategy(UpdateValueStrategy.POLICY_UPDATE),  null);

The variable " obserabedDriverProfile" is a observed selection in a ListViewer, it's in a master-detail scenario. If we add some code like this:
obserabedDriverProfile.addValueChangeListener( new IValueChangeListener(){

            @Override
             public  void handleValueChange(ValueChangeEvent event) {
                DriverProfile dp = (DriverProfile)event.diff.getNewValue();
                driverClassCombo.removeAll();
                driverClassCombo.add(dp.getName();
                driverClassCombo.setText(driverClassCombo.getItem(0));
            }
            
        });
every time you change the selection in the ListViewer,  the selected bean's "name" property will be set to a empty string. Why?  The removeAll method of Combo will clear it's text, and combo's "text" property is bound to selected bean's "name" property. So, the right way is:
obserabedDriverProfile.addValueChangeListener( new IValueChangeListener(){

            @Override
             public  void handleValueChange(ValueChangeEvent event) {
                DriverProfile dp = (DriverProfile)event.diff.getNewValue();
                String name = dp.getName;
                driverClassCombo.removeAll();
                driverClassCombo.add(name);
                driverClassCombo.setText(driverClassCombo.getItem(0));
            }
            
        });

你可能感兴趣的:(Be very careful when using data binding with SWT control.)