开发者

Java swt treeview popup menu

Hiho,

currently I have a working popup me开发者_JAVA百科nu 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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜