使用SWT在树项目上单击填充表行

时间:2013-02-07 08:07:53

标签: java swt jface

单击Tree Item后,是否存在pouplate表行。我不想使用SWT / Jface TreeTableViewer。假设我有一些树,如化妆品,粉末。当我点击装饰时,应在表格中填充相关值。

2 个答案:

答案 0 :(得分:1)

这是一个小例子:

public static void main(String[] args)
{
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, true));

    final Tree tree = new Tree(shell, SWT.SINGLE);
    tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    /* initialize columns */

    TreeItem cosmetics = new TreeItem(tree, SWT.NONE);
    cosmetics.setText(0, "cosmetics");
    /* other text */

    TreeItem powder= new TreeItem(tree, SWT.NONE);
    powder.setText(0, "powder");
    /* other text */


    /* add selection listener to add children */
    tree.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            /* get selection */
            TreeItem[] items = tree.getSelection();

            if(items.length > 0)
            {
                String parent = items[0].getText();
                System.out.println(parent);

                /* add new child */
                TreeItem newItem = new TreeItem(items[0], SWT.NONE);
                newItem.setText(0, "new Item, parent: " + parent);

                /* Expand parent */
                items[0].setExpanded(true);
            }
        }
    });

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

它会为您点击的每个TreeItem添加一个新子项。

答案 1 :(得分:0)

我有解决方案

TreeViewer treeViewer= new TreeViewer(comp);
final TableViewer viewer= new TableViewer(comp1, SWT.BORDER);
treeViewer.addSelectionChangedListener(new ISelectionChangedListener()
{
    public void selectionChanged(SelectionChangedEvent event) 
{ 
    IStructuredSelection selection = (IStructuredSelection) event.getSelection(); 
    Object obj= selection.getFirstElement();
    viewer.setInput(obj);
 } 
});
    }