InsertNickHere
InsertNickHere

Reputation: 151

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);

Upvotes: 2

Views: 1925

Answers (2)

dacwe
dacwe

Reputation: 43504

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...
        }
    }
});

Upvotes: 1

Andreas Dolk
Andreas Dolk

Reputation: 114817

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).

Upvotes: 0

Related Questions