Another question about TreeViewer and PopUp Menu

SWT Designer allows you to create the views, editors, perspectives, pref pages, composites, etc. that comprise Eclipse SWT & RCP applications and plug-ins.

Moderators: Konstantin.Scheglov, gnebling, Alexander.Mitin, jwren, Eric Clayberg

Another question about TreeViewer and PopUp Menu

Postby avier5 » Wed Aug 11, 2004 5:55 am

Hi All,

How can I add to my Tree in the TreeViewer a popup menu?

thanks,

HBC. 8)
avier5
 
Posts: 41
Joined: Thu Jul 22, 2004 12:12 am

Postby admin » Wed Aug 11, 2004 10:13 pm

If you speak about TreeViewer, I think it is better to use JFace MenuManager.
Here is small sample.

Code: Select all
   public void open() {
      final Display display = new Display();
      final Shell shell = new Shell();
      final GridLayout gridLayout = new GridLayout();
      gridLayout.numColumns = 1;
      shell.setLayout(gridLayout);
      shell.setText("SWT Application");
      {
         final TreeViewer treeViewer = new TreeViewer(shell, SWT.BORDER);
         final Tree tree = treeViewer.getTree();
         tree.setLayoutData(new GridData(GridData.FILL_BOTH));
         {
            MenuManager menuMgr = new MenuManager("#PopupMenu");
            menuMgr.setRemoveAllWhenShown(true);
            menuMgr.addMenuListener(new IMenuListener() {
               public void menuAboutToShow(IMenuManager manager) {
                  buildContextMenu(manager);
               }
            });
            Menu menu = menuMgr.createContextMenu(tree);
            tree.setMenu(menu);
         }
      }
      shell.open();
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch())
            display.sleep();
      }
   }
   protected void buildContextMenu(IMenuManager manager) {
      IAction action = new Action("Test action") {
         public void run() {
            System.out.println("Test action executed!");
         }
      };
      manager.add(action);
   }
admin
Moderator
 
Posts: 166
Joined: Thu Jul 24, 2003 12:25 am

Postby avier5 » Thu Aug 12, 2004 1:28 am

Thanks a lot.

HBC.
avier5
 
Posts: 41
Joined: Thu Jul 22, 2004 12:12 am


Return to SWT Designer

Who is online

Users browsing this forum: No registered users and 1 guest