-1
我不知道如何在TreeViewer(JFace)項目上添加鼠標右鍵單擊偵聽器?如何在TreeViewer(JFace)項目上添加鼠標右鍵單擊偵聽器?
MenuManager menuMgr = new MenuManager();
menuMgr.setRemoveAllWhenShown(true);
menuMgr.addMenuListener(new IMenuListener() {
@Override
public void menuAboutToShow(IMenuManager menuManager) {
... add items to menu manager
}
});
Control tree = treeViewer.getControl();
Menu menu = menuMgr.createContextMenu(tree);
tree.setMenu(menu);
這是使用IMenuListener
,您可以添加不同的菜單項:
/*
* Creates the tree.
*
* @return the tree object and create part control
*/
private TreeObject CreateTree() {
TreeParent root = new TreeParent("");
DemoTreeModel ctModel = new DemoTreeModel();
List<String> listType = ctModel.getType();
for (String type : listType) {
TreeParent parentNode1 = new TreeParent(type);
parentNode1.setLevel(1);
List<String> listMachine = ctModel.getName(type);
if (listMachine != null) {
for (String machine : listMachine) {
TreeParent childNode1 = new TreeParent(machine);
childNode1.setLevel(2);
parentNode1.addChild(childNode1);
List<String> listVersion = ctModel.getVersion(machine);
if (listVersion != null) {
for (String ver : listVersion) {
TreeObject version = new TreeObject(ver);
version.setLevel(3);
version.setData(ver);
childNode1.addChild(version);
}
}
}
}
root.addChild(parentNode1);
}
return root;
}
讓我試試吧,非常感謝! – Ace 2014-10-13 08:02:58