Java 在SWT树中获取所有树项

Java 在SWT树中获取所有树项,java,tree,swt,Java,Tree,Swt,我想从我的SWT树中获取所有树元素的数组。但是,树类中包含的方法,getItems()仅返回树的第一级上的项(即,不是任何项的子项) 有人能建议一种获取所有子项/项目的方法吗?的文档非常具体: 返回接收器中包含的(可能为空)项数组,这些项是接收器的直接项子项。这些是树的根 下面是一些实现此技巧的示例代码: public static void main(String[] args) { Display display = new Display(); final Shell sh

我想从我的SWT树中获取所有树元素的数组。但是,树类中包含的方法,
getItems()
仅返回树的第一级上的项(即,不是任何项的子项)

有人能建议一种获取所有子项/项目的方法吗?

的文档非常具体:

返回接收器中包含的(可能为空)项数组,这些项是接收器的直接项子项。这些是树的根

下面是一些实现此技巧的示例代码:

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

    final Tree tree = new Tree(shell, SWT.MULTI);

    TreeItem parentOne = new TreeItem(tree, SWT.NONE);
    parentOne.setText("Parent 1");
    TreeItem parentTwo = new TreeItem(tree, SWT.NONE);
    parentTwo.setText("Parent 2");

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

        item = new TreeItem(parentTwo, SWT.NONE);
        item.setText(parentTwo.getText() + " child " + i);
    }

    parentOne.setExpanded(true);
    parentTwo.setExpanded(true);

    List<TreeItem> allItems = new ArrayList<TreeItem>();

    getAllItems(tree, allItems);

    System.out.println(allItems);

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

private static void getAllItems(Tree tree, List<TreeItem> allItems)
{
    for(TreeItem item : tree.getItems())
    {
        getAllItems(item, allItems);
    }
}

private static void getAllItems(TreeItem currentItem, List<TreeItem> allItems)
{
    TreeItem[] children = currentItem.getItems();

    for(int i = 0; i < children.length; i++)
    {
        allItems.add(children[i]);

        getAllItems(children[i], allItems);
    }
}
publicstaticvoidmain(字符串[]args)
{
显示=新显示();
最终外壳=新外壳(显示);
shell.setText(“StackOverflow”);
setLayout(新的FillLayout());
最终树=新树(shell,SWT.MULTI);
TreeItem parentOne=新的TreeItem(树,SWT.NONE);
parentOne.setText(“父1”);
TreeItem parentTwo=新的TreeItem(tree,SWT.NONE);
parentTwo.setText(“parent2”);
对于(int i=0;i<10;i++)
{
TreeItem=新的TreeItem(parentOne,SWT.NONE);
item.setText(parentOne.getText()+“child”+i);
项=新树项(父项2,SWT.NONE);
item.setText(parentTwo.getText()+“child”+i);
}
parentOne.setExpanded(true);
parentTwo.setExpanded(true);
List allItems=new ArrayList();
getAllItems(树,allItems);
系统输出打印项(allItems);
shell.pack();
shell.open();
而(!shell.isDisposed())
{
如果(!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
私有静态void getAllItems(树、列表allItems)
{
for(TreeItem:tree.getItems())
{
getAllItems(项目,allItems);
}
}
私有静态void getAllItems(TreeItem currentItem,List allItems)
{
TreeItem[]childrent=currentItem.getItems();
for(int i=0;i
的文档非常具体:

返回接收器中包含的(可能为空)项数组,这些项是接收器的直接项子项。这些是树的根

下面是一些实现此技巧的示例代码:

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

    final Tree tree = new Tree(shell, SWT.MULTI);

    TreeItem parentOne = new TreeItem(tree, SWT.NONE);
    parentOne.setText("Parent 1");
    TreeItem parentTwo = new TreeItem(tree, SWT.NONE);
    parentTwo.setText("Parent 2");

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

        item = new TreeItem(parentTwo, SWT.NONE);
        item.setText(parentTwo.getText() + " child " + i);
    }

    parentOne.setExpanded(true);
    parentTwo.setExpanded(true);

    List<TreeItem> allItems = new ArrayList<TreeItem>();

    getAllItems(tree, allItems);

    System.out.println(allItems);

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

private static void getAllItems(Tree tree, List<TreeItem> allItems)
{
    for(TreeItem item : tree.getItems())
    {
        getAllItems(item, allItems);
    }
}

private static void getAllItems(TreeItem currentItem, List<TreeItem> allItems)
{
    TreeItem[] children = currentItem.getItems();

    for(int i = 0; i < children.length; i++)
    {
        allItems.add(children[i]);

        getAllItems(children[i], allItems);
    }
}
publicstaticvoidmain(字符串[]args)
{
显示=新显示();
最终外壳=新外壳(显示);
shell.setText(“StackOverflow”);
setLayout(新的FillLayout());
最终树=新树(shell,SWT.MULTI);
TreeItem parentOne=新的TreeItem(树,SWT.NONE);
parentOne.setText(“父1”);
TreeItem parentTwo=新的TreeItem(tree,SWT.NONE);
parentTwo.setText(“parent2”);
对于(int i=0;i<10;i++)
{
TreeItem=新的TreeItem(parentOne,SWT.NONE);
item.setText(parentOne.getText()+“child”+i);
项=新树项(父项2,SWT.NONE);
item.setText(parentTwo.getText()+“child”+i);
}
parentOne.setExpanded(true);
parentTwo.setExpanded(true);
List allItems=new ArrayList();
getAllItems(树,allItems);
系统输出打印项(allItems);
shell.pack();
shell.open();
而(!shell.isDisposed())
{
如果(!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
私有静态void getAllItems(树、列表allItems)
{
for(TreeItem:tree.getItems())
{
getAllItems(项目,allItems);
}
}
私有静态void getAllItems(TreeItem currentItem,List allItems)
{
TreeItem[]childrent=currentItem.getItems();
for(int i=0;i
回答得很好,但请注意allItems列表不包括tree.getItems()返回的根元素。注意:要在@HennoVermeulen和all中实现它,只需添加
allItems.add(item)
作为
#getAllItems(Tree,List)
@Sumit内循环的第一条语句,我发布的代码不限于特定级别。它可以处理任意数量的级别。@Baz:谢谢,你能帮我吗?@Sumit很抱歉,我不明白你的问题,不久前我已经投票决定以“不清楚你在问什么”结束。如果你能重写你的问题,让它更清楚你想做什么,那么也许人们会提供帮助。这个问题目前还不清楚。答案很好,但请注意allItems列表不包括tree.getItems()返回的根元素。注意:要实现@HennoverMelen和all,只需添加
allItems.add(item)
作为
#getAllItems(Tree,List)
@Sumit内循环的第一条语句,我发布的代码不限于特定级别。它可以处理任意数量的级别。@Baz:谢谢,你能帮我吗?@Sumit很抱歉,我不明白你的问题,不久前我已经投票决定以“不清楚你在问什么”结束。如果你能重写你的问题,让它更清楚你想做什么,那么也许人们会提供帮助。目前的问题根本不清楚。