我想在jtree中根據哪個節點右鍵點擊一個條件彈出菜單。可能嗎?我實現它如下,但如果我點擊深度級別1節點首先它顯示正確的彈出菜單,但之後,如果我右鍵點擊節點級別2我仍然得到相同的彈出菜單級別1和反之亦然。jtree中的條件彈出式菜單
DefaultMutableTreeNode node = (DefaultMutableTreeNode) pmTree.getLastSelectedPathComponent();
popup = new JPopupMenu();
popup.setInvoker(pmTree);
PopupHandler handler = new PopupHandler(pmTree, popup);
if(node.getLevel() == 1)
{
popup.add(getMenuItem("Start a VM", handler));
popup.add(getMenuItem("Monitor all VMs", handler));
}
else if(node.getLevel() == 2)
{
popup.add(getMenuItem("Change VM configuration", handler));
popup.add(getMenuItem("Monitor VM", handler));
popup.add(getMenuItem("Migrate VM", handler));
popup.add(getMenuItem("Show VM Configuration", handler));
popup.add(getMenuItem("Stop VM", handler));
}