Community
Participate
Working Groups
I20071213-0010 I am modifying the PDE product editor (bug 213542) and am having trouble getting the combo boxes to work correctly. My combo boxes are ComboParts a wrapper around a CCombo. Originally items were added to the combo using setItems(String[]) and the selection was checked using getSelection(). After my changes, I was adding items using add(String) and checking the selection using getSelectionIndex(). getSelectionIndex is always returning -1. If I select different things in the combo, getSelection() will return the proper String, but the selection index does not change. I compared against similar code in Debug and the same steps work fine if a Combo is used instead. I'm guessing that this has something to do with the text/list combination where I'm getting the text selection index. However, the javadoc says that it will return the selection from the List. Debugging the editor I saw that add(String) appeared to work correctly as the item count in the List inside the CCombo is updated. However, the list.getSelectionIndex() call inside of CCombo always returns -1.
Sorry, too many bugs open, this is causing me problems on bug 172793.
The steps you describe work in the snippet below, so there must be more context required to reproduce the problem. Are you able to modify the snippet below or provide a new one from scratch that shows the problem happening? public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setBounds(10,10,200,200); final CCombo combo = new CCombo(shell, SWT.NONE); combo.setBounds(10,10,100,30); combo.add("item 0"); combo.add("item 1"); combo.add("item 2"); combo.add("item 3"); combo.add("item 4"); combo.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { System.out.println("index: " + combo.getSelectionIndex()); } }); shell.open(); while(!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
Snippet works for me as well, so there must be something else affecting it. I will investigate further.
The difference causing the problem is that the Product Editor I was modifying uses modify listeners instead of selection listeners. With a selection listener the index is correct, but when in a modify listener, the index is always -1. combo.addModifyListener(new ModifyListener() { public void modifyText(ModifyEvent e) { System.out.println("index: " + combo.getSelectionIndex()); } }); I'm not sure why modify listeners are being added, as the combos are set to read only. Hopefully this means fixing my problem is as simple as changing the listeners. This bug can probably be closed. It wasn't immediately obvious to me that the modification listener would always change the selection index to -1 (even if I hadn't changed the text). However, after figuring it out, it sounds like reasonable behaviour.
I get the correct behaviour when using SelectionListener, marking as WORKSFORME.