Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用TreeCursor在SWT树中选择单个单元格_Java_Eclipse_Swt - Fatal编程技术网

Java 如何使用TreeCursor在SWT树中选择单个单元格

Java 如何使用TreeCursor在SWT树中选择单个单元格,java,eclipse,swt,Java,Eclipse,Swt,首先,我很惊讶TreeCursor.setSelection(treeitemrow,int column)没有选择任何内容。然后我查看了这个方法的源代码,发现它们调用Tree.indexOf()。当然,如果树项不是树的直接子项,它将找不到该树项。 我不明白这个类的用法吗?是否可以选择不是树的直接子级的树项目?我几乎不相信选择功能如此有限。我的意思是一棵树是一棵树,通常有一个根和很深的层次结构 import org.eclipse.debug.internal.ui.viewers.model.

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

我不确定是否理解您的问题,但此代码对我有效,并选择了一个不是根的直接子级的
树项:

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();
}
publicstaticvoidmain(字符串[]args)
{
Display=Display.getDefault();
最终外壳=新外壳(显示);
shell.setText(“StackOverflow”);
setLayout(新的FillLayout());
Tree Tree=新树(shell,SWT.NONE);
TreeItem parent=新的TreeItem(树,SWT.NONE);
parent.setText(“根”);
对于(int i=0;i<10;i++)
{
TreeItem子项=新的TreeItem(父项,SWT.NONE);
child.setText(“child”+i);
父母=子女;
}
tree.setSelection(父级);
shell.pack();
shell.open();
而(!shell.isDisposed())
{
如果(!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
运行时看起来是这样的:


好的,我找到了解决办法。我从
org.eclipse.debug.internal.ui.viewers.model.TreeCursor
切换到
org.eclipse.swt.custom.TreeCursor
(自Eclipse3.8以来就存在),问题得到了解决。

你说的“选择一个不是树的直接子项”是什么意思?该项怎么可能不是树的子项?您是指根的直接子级吗?对于“树的直接子级”,我指的是使用新的TreeItem(tree,SWT.NONE)创建的项;使用此构造函数可以创建多个“根”,这是错误的,因为树只能有一个根。这就是为什么我没有使用这个词。我想使用TreeCursor.setSelection(TreeItem row,int column)来选择列。