简体   繁体   中英

Java swt treeview popup menu

Hiho,

currently I have a working popup menu which appears when I click on a treeview item. But I want to show different popups for different tree view entries. I don't get a idea how to do so...

Here is my code for creating the menu:

 MenuManager menuMgr = new MenuManager("#PopupMenu"); 
 menuMgr.setRemoveAllWhenShown(true);
 menuMgr.addMenuListener(new IMenuListener() {
     @Override
     public void menuAboutToShow(IMenuManager manager) {
         Action action = new Action() {
      public void run() {
                // So something
      }
  };
  action.setText("Set as working file");
  manager.add(action);
 }

 });

 Menu menu = menuMgr.createContextMenu(getTree());
 getTree().setMenu(menu);

You should propably use a MouseListener on the tree:

final Tree tree = new Tree(parent, ...);
tree.addMouseListener(new MouseAdapter() {
    @override
    public void mouseDown(MouseEvent me) {
        if(tree.getSelection() instanceof MySpecificTreeNode) {
            // create menu...
        }
    }
});

Two ideas. For both you need to listen to selections on the TreeView, because that's the only way to determine which Menu (or special content) you want to show.

Then you could either set the correct menu to the the tree right after you know which one to use or contribute the needed items to the existing menu (that's how it's done in the eclipse framework).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM