Java SWT / JFace - TreeViewer - Different Menus for Parent and Child - Code Snippet

treeViewer.getTree().addMenuDetectListener(new MenuDetectListener() {

public void menuDetected(MenuDetectEvent e) {

// get the selected items
                                ISelection selection = (IStructuredSelection) treeViewer.getSelection();

            // define menus - menu 1 if right click on parent tree item
Menu menu1 = new Menu(treeViewer.getTree());
       MenuItem item = new MenuItem(menu1, SWT.PUSH);
       item.setText("Test 1");
       item.addSelectionListener(new Test1Listener());
           // menu 2 if right click on child tree item
        Menu menu2 = new Menu(procedureTreeViewerICD9.getTree());
       MenuItem item1 = new MenuItem(menu2, SWT.PUSH);
       item1.addSelectionListener(new Test2Listener());

           // menu 3 if right click on neither the parent tree item or child tree item. ex - menu for headers.
           Menu menu3 = new Menu(procedureTreeViewerICD9.getTree());
        MenuItem item1 = new MenuItem(menu3, SWT.PUSH);
        item1.addSelectionListener(new Test3Listener());
                               // check if the tree item selected is parent or child and set the menu accordingly
       if (selection.getFirstElement() instanceof <ParentClass> ) {
} else if (selection.getFirstElement() instanceof <ChildClass> )
        } else {
           // clear the selection
Subscribe to Java News and Posts. Get latest updates and posts on Java from
Enter your email address:
Delivered by FeedBurner
comments powered by Disqus