首先,我很惊讶TreeCursor.setSelection(TreeItem row, int column)
没有选择任何东西。然后我查看了这个方法的来源,发现他们调用了Tree.indexOf()
. 当然,如果它不是树的直接子项,它就不会找到树项。我不明白这个类的用法吗?是否可以选择不是树的直接子级的树项?我几乎不相信选择功能是如此有限。我的意思是一棵树就是一棵树,通常有一个根和深层次......
import org.eclipse.debug.internal.ui.viewers.model.TreeCursor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Tree tree = new Tree(shell, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION);
tree.setHeaderVisible(true);
TreeColumn column1 = new TreeColumn(tree, SWT.LEFT);
column1.setText("Column 1");
column1.setWidth(200);
TreeColumn column2 = new TreeColumn(tree, SWT.CENTER);
column2.setText("Column 2");
column2.setWidth(200);
TreeColumn column3 = new TreeColumn(tree, SWT.RIGHT);
column3.setText("Column 3");
column3.setWidth(200);
TreeColumn column4 = new TreeColumn(tree, SWT.RIGHT);
column4.setText("Column 4");
column4.setWidth(200);
TreeItem root = new TreeItem(tree, SWT.NONE);
root.setText(new String[] { "root", "a1", "b1", "c1" });
TreeItem rootChild1 = new TreeItem(root, SWT.NONE);
rootChild1.setText(new String[] { "rootChild1", "a2", "b2", "c2" });
TreeItem rootChild2 = new TreeItem(root, SWT.NONE);
rootChild2.setText(new String[] { "rootChild2", "a2", "b2", "c2" });
TreeCursor cursor = new TreeCursor(tree, SWT.NONE);
// cursor.setSelection(root, 2);
cursor.setSelection(rootChild1, 2);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
如果取消注释“cursor.setSelection(root, 2);”行 您将看到选择按预期工作。