简体   繁体   English

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

[英]Populate table row on tree item click using SWT

Is there anyway to pouplate table row after clicking Tree Item.?I don't want to use SWT/Jface TreeTableViewer. 在单击“树项”后,是否仍有必要对表行进行聚合?我不想使用SWT / Jface TreeTableViewer。 Suppose i have Tree with some tree items like cosmetics, Powder. 假设我的树上有一些树,例如化妆品,粉。 When i click on cosmetic, related value should be populated in table. 当我单击化妆品时,应该在表中填充相关值。

Here is a small example: 这是一个小例子:

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();
}

It will add a new child to every TreeItem you click on. 它将为您单击的每个TreeItem添加一个新的子项。

i have got the solution 我有解决方案

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);
 } 
});
    } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM