Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » [Databinding] ComboViewer default selection
[Databinding] ComboViewer default selection [message #558686] Tue, 14 September 2010 01:30 Go to next message
Hugo Herrera is currently offline Hugo HerreraFriend
Messages: 20
Registered: July 2009
Junior Member
Hola,

need help with databinding.

I have an application with a ComboViewer listing country states. When a new Person is created, ComboViewer is working as expected and State is properly saved in Person. The problem is when opening for editing, the ComboViewer is not listing the saved State as the defaulted selected item. It is displaying blanks. I tried using ComboViewer.setSelection() but same results. Any Idea? am i doing the binding correctly?

TextFields are working fine with binding.

Here is the code.



class Person {
String name;
State state;
...
}

class State {
Integer id;
String name;
...
}


Person customer;
List<State> states = sql-list-of-states;

Label stateLabel = new Label(composite2, SWT.NONE);
stateLabel.setText("State:");
Combo stateCombo = new Combo(composite2, SWT.READ_ONLY);
ComboViewer stateComboViewer = new ComboViewer(stateCombo);
stateComboViewer.setContentProvider(new ArrayContentProvider());
stateComboViewer.setInput(states);

doBindValue(stateComboViewer, "state");
....


private void doBindValue(ComboViewer comboViewer, String propertyName) {
IObservableValue model = BeansObservables.observeValue(customer,
propertyName);

dbc.bindValue(ViewersObservables. observeSingleSelection(comboViewer), model);
model.addChangeListener(dirtyChangeListener);
}



Re: [Databinding] ComboViewer default selection [message #559052 is a reply to message #558686] Wed, 15 September 2010 12:59 Go to previous messageGo to next message
Hugo Herrera is currently offline Hugo HerreraFriend
Messages: 20
Registered: July 2009
Junior Member
Problem solved by adding the following code to the State entity class.

@Override
public boolean equals(Object obj) {
if (obj instanceof State{
State p = (State) obj;
if (this.id.equals(p.getId()))
return true;
}
return super.equals(obj);
}
}


Cheers.
Re: [Databinding] ComboViewer default selection [message #559067 is a reply to message #559052] Wed, 15 September 2010 13:53 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
You'd better set an IElementComparer on the Viewer because overwriting
equals/hashCode can have undesired effects (e.g. in HashMaps) when the
variable you are using is mutable.

Tom

Am 15.09.10 14:59, schrieb Hugo Herrera:
> Problem solved by adding the following code to the State entity class.
>
> @Override
> public boolean equals(Object obj) {
> if (obj instanceof State{
> State p = (State) obj;
> if (this.id.equals(p.getId()))
> return true;
> }
> return super.equals(obj);
> }
> }
>
>
> Cheers.
Re: [Databinding] ComboViewer default selection [message #559414 is a reply to message #559067] Thu, 16 September 2010 14:52 Go to previous message
Hugo Herrera is currently offline Hugo HerreraFriend
Messages: 20
Registered: July 2009
Junior Member
Code updated to use IElementComparer now.

Thank you Tom.
Previous Topic:[DataBinding] Removing several elements and ObservableListContentProvider
Next Topic:Preference dialog lazy page loading
Goto Forum:
  


Current Time: Fri Mar 29 12:37:41 GMT 2024

Powered by FUDForum. Page generated in 0.03405 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top