RAP 2.3 - keeping selected value on ComboViewer [message #1421372] |
Thu, 11 September 2014 05:21  |
Eclipse User |
|
|
|
Hello everyone,
i have an problem with eclipse rap 2.3 and comboviewer.
When a value is selected in my combobox and the content of these combo is changed i like to keep the old selection if the new values contains the old selected item.
here my example code:
i create an combo:
final ComboViewer viewer = new ComboViewer(parent, SWT.NONE);
viewer.setContentProvider(new ArrayContentProvider());
for demonstrating this example i created two buttons:
Button btnObj = new Button(parent, SWT.NONE);
btnObj.setText("change input");
btnObj.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
String[] itemsArr = new String[] {"a", "b", "c", "d", "e" };
String oldValue = viewer.getCombo().getText();
viewer.setInput(Arrays.asList(itemsArr));
for(String strObj : viewer.getCombo().getItems()) {
if(strObj.equals(oldValue)) {
viewer.getCombo().setText(oldValue);
}
}
System.out.println("viewer.getCombo().getSelectionIndex() " + viewer.getCombo().getSelectionIndex());
System.out.println("viewer.getCombo().getText() " + viewer.getCombo().getText());
}
});
Button btn2Obj = new Button(parent, SWT.NONE);
btn2Obj.setText("change input 2");
btn2Obj.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
String[] itemsArr = new String[] {"a", "b", "c", "d", "e", "f" };
String oldValue = viewer.getCombo().getText();
viewer.setInput(Arrays.asList(itemsArr));
for(String strObj : viewer.getCombo().getItems()) {
if(strObj.equals(oldValue)) {
viewer.getCombo().setText(oldValue);
}
}
System.out.println("viewer.getCombo().getSelectionIndex() " + viewer.getCombo().getSelectionIndex());
System.out.println("viewer.getCombo().getText() " + viewer.getCombo().getText());
}
});
now when i run this code and push button 'change input' it fills my combo with that list of data. After when i select one item, for example 'c', and press this button again the value in my combo is 'c'.
Now when i push the second button 'change input 2' it fills the second list in my combo, but there is no item selected. The console prints the following:
viewer.getCombo().getSelectionIndex() -1
viewer.getCombo().getText()
viewer.getCombo().getSelectionIndex() 2
viewer.getCombo().getText() c
viewer.getCombo().getSelectionIndex() 2
viewer.getCombo().getText() c
so it seems that the value 'c' is still selected, but in the ui there ist nothing selected. This problem only occurs when the content lengt of the combo list changes. For example when the second buttons loads the items "e", "d", "c", "b", "a" into the combo it works, that means when i select for example 'b' and press both buttons it keeps that selection in the ui.
Can anybody tell my what's wrong with my code?
Best regards
Christian Wrobel
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.06649 seconds