Share

Search Java Code Snippets


  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.





Java - Code Samples

 Sample 1. SWT / JFace - TreeViewer - Different Menus for Parent and Child

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

   @Override
   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.setText("Test2");
           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.setText("Test3");
           item1.addSelectionListener(new Test3Listener());
         
                              
                               // check if the tree item selected is parent or child and set the menu accordingly
          if (selection.getFirstElement() instanceof <ParentClass> ) {
              treeViewer.getControl().setMenu(menu1);
           } else if (selection.getFirstElement() instanceof <ChildClass> )
              treeViewer.getControl().setMenu(menu2);
           } else {
               treeViewer.getControl().setMenu(menu3);
           }
           // clear the selection
           treeViewer.setSelection(null);
   }
});

   Like      Feedback     swt  jface  jface tree   jface table  jface menu   MenuDetectEvent



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus