Project: SWT
Bug 46702 List.getFocusIndex() does not answer -1
Reported: 2003-11-14 15:45:43 | Status: resolved fixed

Run the snippet below, click on the empty list, and it prints its focus index as 0 even though it has no items. Same happens if you have a list with items and then do a List.removeAll(). According to the javadoc these should answer - 1, which is what (for instance) gtk does. public static void main(String[] args){ Display display = new Display(); Shell shell = new Shell(display); shell.setBounds(10,10,100,100); final List list = new List(shell,SWT.SINGLE); // SWT.MULTI does the same list.setBounds(10,10,80,50); list.addListener(SWT.MouseDown, new Listener() { public void handleEvent(Event event) { System.out.println(list.getFocusIndex()); } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } Looking at the implementation of getFocusIndex(), I don't think that LB_GETCARETINDEX ever answers -1. Perhaps we should just answer -1 whenever the list contains no items.

Files changed in fix:
bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/List.java