I want a conditional popup menu in jtree based upon which node i right click upon. Is it possible? I implemented it as follows but if I click on node at depth level 1 first it shows correct popup menu but then after if I right click on node at level 2 I still get the same popup menu as for level 1. And similarly vice versa.
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));
}
This question appears to be relevant to your situation:
JTree and dropdown options on right clicking nodes
Can you post more of your code?
Its hard to tell without more context but I assume the problem is that the menu is getting built and initialized on the first click but not replaced on the subsequent click.