Project: SWT
Bug 264255 Tree selection may not be preserved when creating/disposing items
Reported: 2009-02-09 16:02:34 | Status: resolved fixed

- run the snippet below - expand the root item - select "item 1" - press any key --> disposes "item 8" --> note that the selection moved from "item 1" up to "item 0" - to see another case of the same problem, comment out the snippet's "root.getItem(7).dispose();" line and uncomment the line below it --> this causes the selection to move down one line when the new item is created below public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setBounds(10, 10, 300, 300); shell.setLayout(new FillLayout()); final Tree tree = new Tree(shell, SWT.NONE); final TreeItem root = new TreeItem(tree, SWT.NONE); root.setText("root"); for (int i = 0; i < 8; i++) { new TreeItem(root, SWT.NONE).setText("item " + i); } tree.addListener(SWT.KeyDown, new Listener() { public void handleEvent(Event event) { System.out.println("old selection: " + tree.getSelection()[0]); //root.getItem(7).dispose(); new TreeItem(root, SWT.NONE, 7).setText("new item"); System.out.println("new selection: " + tree.getSelection()[0]); } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }

Files changed in fix:
bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Tree.java