In order to get the text of selected TreeItems from a Tree in SWT you can use the following method.

The Tree class already offers a nice function getSelection() which returns an array holding all selected TreeItems.

Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
 * Returns the text of selected TreeItems in a provided Tree.
 
 * @param tree
 *            a Tree with selected items.
 * @return an array of Strings with the text values of selected TreeItems in
 *         provided Tree.
 */
private String[] getSelectedTreeItems(Tree tree) {
	String[] ret = null;
 
	TreeItem[] items = tree.getSelection();
	ret = new String[items.length];
	for (int i = 0; i < items.length; i++) {
		ret[i] = items[i].getText();
	}
 
	return ret;
}