2 u011189813 u011189813 于 2015.06.15 11:05 提问

swt tree的获取item值的问题 10C

// 获取JsonTree对象遍历Tree控件
public static void getChild(Tree tree, JsonTree jTree)
{

    final TreeItem item = new TreeItem(tree, SWT.None);
    String itemText = jTree.getId();
    String path = (jTree.getPid() == "/" ? jTree.getPid(): jTree.getPid() + "/") + jTree.getId();
    String [] strings ={jTree.getId(), path};
    if (!"/".equals(itemText))
    {
        item.setText(strings);
    }
    if (jTree.isChild)
    {
        List<JsonTree> jsonTree = jTree.getList();
        if (null != jsonTree && !jsonTree.isEmpty())
        {
            for (JsonTree childData : jsonTree)
            {
                getChild(item, childData);
            }
        }
    }
}

// 获取JsonTree对象遍历TreeItem控件
private static void getChild(TreeItem treeItem, JsonTree jTree)
{

    final TreeItem item = new TreeItem(treeItem, SWT.None);
    String itemText = jTree.getId();
    String path = (jTree.getPid() == "/" ? jTree.getPid(): jTree.getPid() + "/") + jTree.getId();
    String [] strings ={jTree.getId(), path};
    if (!"/".equals(itemText))
    {
        item.setText(strings);
    }
    if (jTree.isChild)
    {
        List<JsonTree> jsonTree = jTree.getList();
        if (null != jsonTree && !jsonTree.isEmpty())
        {
            for (JsonTree childData : jsonTree)
            {
                getChild(item, childData);
            }
        }
    }

tree.addSelectionListener(new SelectionListener()
{

                                    public void widgetSelected(SelectionEvent e)
                                    {
                                        TreeItem[] selection = tree.getSelection();
                                        String text = selection[0].getText(1);
                                        System.out.println("========"+text);
                                    }

                                    public void widgetDefaultSelected(SelectionEvent e)
                                    {
                                    }
                                });

//我在向TreeItem中set值,为一个数组,遍历tree时打印可以看到有item.getText(1)的值,但是在点击tree触发事件时为什么得不到item.getText(1)的值?


1个回答

u010697684
u010697684   2017.02.23 09:40

settext属性的多个值,适用于多列的tree。要想在每个节点上存储别的属性信息,请使用 setData。

Csdn user default icon
上传中...
上传图片
插入图片