Associate PopupMenu with an SWT TreeItem

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

Associate PopupMenu with an SWT TreeItem

Postby cochand » Fri Jul 15, 2005 12:25 pm

I have an SWT Tree with TreeItems. I can left mouse click on the tree and display different Composites in a StackLayout within my app. I want right mouse clicks on the TreeItems to popup various menus. Different popup menus will appear based on which node of the tree is active and/or clicked on with the right mouse.

How should I structure this in SWT Designer?

Should I use create a Popup Menu under Menu Controls? Should that PopupMenu be created within its own Composite or Shell?

Should I be calling tree.setMenu, with different menu parameters, based on which node I click on in the tree?

Thanks for pointing me in the right direction.
cochand
 
Posts: 17
Joined: Wed Jul 06, 2005 1:20 pm

Re: Associate PopupMenu with an SWT TreeItem

Postby Eric Clayberg » Fri Jul 15, 2005 2:36 pm

cochand wrote:I have an SWT Tree with TreeItems. I can left mouse click on the tree and display different Composites in a StackLayout within my app. I want right mouse clicks on the TreeItems to popup various menus. Different popup menus will appear based on which node of the tree is active and/or clicked on with the right mouse. How should I structure this in SWT Designer? Should I use create a Popup Menu under Menu Controls? Should that PopupMenu be created within its own Composite or Shell? Should I be calling tree.setMenu, with different menu parameters, based on which node I click on in the tree?

You can't add a popup menu to a TreeItem, but you can add a popup menu to the Tree widget itself and dynamically configure it based on the selection in the Tree.

You can either add/remove menu items or add all of them up front and then enable/disable them as needed.

Here's a simple example:

Code: Select all
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MenuAdapter;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
public class ABC {
   private Tree tree;
   protected Shell shell;
   public static void main(String[] args) {
      try {
         ABC window = new ABC();
         window.open();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
   public void open() {
      final Display display = Display.getDefault();
      createContents();
      shell.open();
      shell.layout();
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch())
            display.sleep();
      }
   }
   protected void createContents() {
      shell = new Shell();
      shell.setLayout(new FillLayout());
      shell.setSize(300, 200);
      shell.setText("Dynamic Menu Example");
      tree = new Tree(shell, SWT.BORDER);
      final TreeItem item1 = new TreeItem(tree, SWT.NONE);
      item1.setText("First");
      final TreeItem item2 = new TreeItem(tree, SWT.NONE);
      item2.setText("Second");
      final Menu menu = new Menu(tree);
      tree.setMenu(menu);
      menu.addMenuListener(new MenuAdapter() {
         public void menuShown(MenuEvent e) {
            // Get rid of existing menu items
            MenuItem[] items = menu.getItems();
            for (int i = 0; i < items.length; i++) {
               ((MenuItem) items[i]).dispose();
            }
            // Add menu items for current selection
            MenuItem newItem = new MenuItem(menu, SWT.NONE);
            newItem.setText("Menu for " + tree.getSelection()[0].getText());
         }
      });
   }
}
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA

Postby cochand » Fri Jul 15, 2005 3:41 pm

Thanks for the help!
cochand
 
Posts: 17
Joined: Wed Jul 06, 2005 1:20 pm


Return to SWT Designer

Who is online

Users browsing this forum: No registered users and 1 guest