本文介绍了在SWT树中添加右键单击菜单以进行树状设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在SWT树中向右键单击弹出菜单中添加树状图。每个treeitem都应该在其上启用右键单击

How can I add a right click popup menu to a treeitem in a SWT Tree. Each treeitem should have a right click enabled on it

推荐答案

只需使用 tree.setMenu(Menu)

你去:

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

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

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

        for(int j = 0; j < 3; j++)
        {
            TreeItem child = new TreeItem(item, SWT.NONE);
            child.setText("Child " + i + " " + j);
        }
    }

    final Menu menu = new Menu(tree);
    tree.setMenu(menu);
    menu.addMenuListener(new MenuAdapter()
    {
        public void menuShown(MenuEvent e)
        {
            MenuItem[] items = menu.getItems();
            for (int i = 0; i < items.length; i++)
            {
                items[i].dispose();
            }
            MenuItem newItem = new MenuItem(menu, SWT.NONE);
            newItem.setText("Menu for " + tree.getSelection()[0].getText());
        }
    });

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

这篇关于在SWT树中添加右键单击菜单以进行树状设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 09:10