0

首先,我很惊讶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);”行 您将看到选择按预期工作。

4

2 回答 2

0

好的,我找到了解决方案。我从 to 切换org.eclipse.debug.internal.ui.viewers.model.TreeCursororg.eclipse.swt.custom.TreeCursor(自 Eclipse 3.8 起就存在)并且问题得到了解决。

于 2013-04-17T09:50:07.393 回答
0

我不太确定我是否理解您的问题,但此代码适用于我并选择一个TreeItem不是根的直接子级的代码:

public static void main(String[] args)
{
    Display display = Display.getDefault();
    final Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new FillLayout());

    Tree tree = new Tree(shell, SWT.NONE);

    TreeItem parent = new TreeItem(tree, SWT.NONE);
    parent.setText("root");

    for(int i = 0; i < 10; i++)
    {
        TreeItem child = new TreeItem(parent, SWT.NONE);
        child.setText("child " + i);

        parent = child;
    }

    tree.setSelection(parent);

    shell.pack();
    shell.open();
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

运行时是这样的:

在此处输入图像描述

于 2013-04-17T08:55:11.483 回答