先看一下效果:
如图:在树上单击鼠标右键会弹出 弹出式菜单。做法其实很简单,先做一个树:
final TreeViewer treeViewer = new TreeViewer(group, SWT.BORDER); tree = treeViewer.getTree(); tree.setBounds(0, 10, 192, 354); treeViewer.setContentProvider(new TreeContentProvider()); treeViewer.setLabelProvider(new TreeLabelProvider()); initPopup(); //主要是这个函数,将tree生成后,实现这个函数就行了。
public void initPopup(){ Menu menu=new Menu(tree); MenuItem newItem=new MenuItem(menu,SWT.PUSH); newItem.setText("新增部门"); MenuItem newMemberItem=new MenuItem(menu, SWT.PUSH); newMemberItem.setText("新增员工"); MenuItem editItem=new MenuItem(menu,SWT.PUSH); editItem.setText("编辑"); MenuItem deleteItem=new MenuItem(menu, SWT.PUSH); deleteItem.setText("删除"); tree.setMenu(menu); }
若想给MenuItem添加响应:
public void initPopup(){ Menu menu=new Menu(tree); MenuItem addNode=new MenuItem(menu,SWT.PUSH); addNode.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { } }); addNode.setText("添加节点"); MenuItem deleteNode=new MenuItem(menu, SWT.PUSH); deleteNode.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { } }); deleteNode.setText("删除节点"); tree.setMenu(menu); }